Jump to content

Custom console command not working


Recommended Posts

function GLOBAL.radio1(speech1)

    for k,v in pairs(Ents) do 
        if v.prefab == "grass" then 
            v.components.talker:Say(speech1) 
        end 
    end

end

For context, grass already has talker component. 

This is in my modmain right now, and I'm trying to make it so all the grass will say whatever is typed into "speech1".

like
radio1("hi")

I keep getting:
bad argument #1 to 'pairs' (table expected, got nil) 

in the console whenever I try that.

 

But typing     

 for k,v in pairs(Ents) do 
        if v.prefab == "grass" then 
            v.components.talker:Say("hi") 
        end 
    end

into the in-game console works just fine.

I'm probably missing something obvious or messing something up.

Thanks.

Link to comment
Share on other sites

How are you adding the component "talker" to Grass? This is one of the few components that needs to be on the client too.

How are you running the command? If you're hosting with Caves, you'll see it says either Local or Remote on the left side of the box where you type text. Make sure it's sent as Remote, meaning as the server. You toggle between them with CTRL.

  • Thanks 1
Link to comment
Share on other sites

Ah thanks.

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 25
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(0.2, 0.4, 0.8)
    inst.components.talker.offset = Vector3(0, 0, 0)

What should I do for it to appear for the clients?

Link to comment
Share on other sites

7 hours ago, certif said:

What should I do for it to appear for the clients?

If you want to add the component to clients too, just put the code before the usual "if not TheWorld.ismastersim return inst end", rather than after. That's what determines if something is both on client and server (before) or only server (after).

 

  • Thanks 1
Link to comment
Share on other sites

I just tested it with another player in the server, it works fine for me, they have the mod and they can see it. Here's my code in modmain:

AddPrefabPostInit("grass", function(inst)
	inst:AddComponent("talker")
	
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
end)

function GLOBAL.radio1(speech1)
	for k,v in pairs(GLOBAL.Ents) do
		if v.prefab == "grass" then
			v.components.talker:Say(speech1)
		end
	end
end

I'll reiterate on this point though, it's very important or only you will see the text:

On 11/11/2023 at 5:56 PM, ClumsyPenny said:

How are you running the command? If you're hosting with Caves, you'll see it says either Local or Remote on the left side of the box where you type text. Make sure it's sent as Remote, meaning as the server. You toggle between them with CTRL.

  • Thanks 1
Link to comment
Share on other sites

Ah I fixed it.
I had changed the prefab from grass to radio1 (after i got your advice about global.theworld.ismastersim) and the radio1 was in a container. The players couldn't see the dialog from the container, but they could see it again when I took it out of the chest and dropped it on the ground. 
The container was the issue.

Sorry about that!
And thanks for helping so much!

  • GL Happy 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...