Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Chat code not working? I don't think the event is firing

So, I tried to make a little chat-thing script. Once you look at the script you may understand. (There is an event in serverstorage.)

Event - Firer script. (LocalScript)

game.Players.PlayerAdded:Connect(function()
	game.ServerStorage.ChatEvents:FireServer("Ah, I have a headache... I can't see...")
end)

Detector script in the textlabel . (Normal script)

game.ServerStorage.ChatEvents.OnServerEvent:Connect(function(sentence)
	local oof = tostring(sentence) -- I do this for safety
	script.Parent:TweenPosition(UDim2.new(0.25, 0, 0.85, 0), "In", "Linear", 0.2)
	for i = 1, #oof do
		script.Parent.Text = string.sub(oof, 1, i)
	end
	script.Parent:TweenPosition(UDim2.new(0.25, 0, 1.2, 0), "Out", "Linear", 0.2)
	script.Parent.Text = " "
end)

9 comments

0

Use the print() command under each line of code to see if it's firing.

0

OK let me try...

0

Apparently the "PlayerAdded" function is not working.

0

Any reasons for it?

0

The reason that's happening is that the player is loading in before the script does.

0

So how do I fix it? I'm sorry, I'm a bit new to this kind of scripting. (Not new to scripting though xD )

0

in this particular case, you cannot use PlayerAdded in a local script

0

also remember you must always do player,sentence instead of just sentence

0

hmm. Thanks. I'll get to it soon! thanks, bye

2 answers

✓ Accepted answer

edit like cows pointed out you need to move your remote to somewhere the client can use it such as the replicated storage. localscripts can't see the server storage

You are trying to use the player added event in a local script. Localscripts run when the player joins the game, or when the players character loads in depending on where you put them

This means when the player joins the game, the player added event will be made, but it will not recognize the player since the player already loaded into the game before the event was created.

Instead, you can put only this line of code in the local script and it'll run. (Also pls let me know where this script is located its important)

game.ServerStorage.ChatEvents:FireServer("Ah, I have a headache... I can't see...")

For your normal script, code should be the same, but instead of only having sentence in the parameter, we need to pass in the player and sentence:

game.ServerStorage.ChatEvents.OnServerEvent:Connect(function(player, sentence)
    local oof = tostring(sentence) -- I do this for safety
    script.Parent:TweenPosition(UDim2.new(0.25, 0, 0.85, 0), "In", "Linear", 0.2)
    for i = 1, #oof do
        script.Parent.Text = string.sub(oof, 1, i)
    end
    script.Parent:TweenPosition(UDim2.new(0.25, 0, 1.2, 0), "Out", "Linear", 0.2)
    script.Parent.Text = " "
end)

It seems like you're tweening some sort of gui whenever a player joins the game. Is this a billboard or surface gui? Is it the players screen gui?

6 comments

0

I'll get to it soon, rn im doing another part of my game, thanks tho!

0

kk

0

Well, I did a completely different method, and apparently it worked... (I did put the event in replicatedStorage though, so I will accept your answer :)

0

Also, question, did roblox remove FireClient ?

0

It's not showing up on my LocalScript

0

are you sure your remote is a remote event? bindables and remotes look similar.

There are several issues here. The first issue is that your RemoteEvent is stored inside of ServerStorage. It's called ServerStorage because only the Server can access it. That means when you try to fire the event from a LocalScript, the LocalScript can't actually access the remote to do so. Putting the RemoteEvent in ReplicatedStorage instead would work, as children of ReplicatedStorage do replicate to the client.

The next issue is your choice of script levels. When dealing with GUIs, it is always best to use a LocalScript because the GUI is only visible to the client anyway. Additionally, what you have in a LocalScript right now should be in a regular script because there's nothing that can't be done on the server. I'll write out what the script should look like, but it's important you know why these changes are being made so that you don't make the same mistakes in the future.

--Server script, located ideally in ServerScriptService
game.Players.PlayerAdded:Connect(function(player)
	game.ReplicatedStorage.ChatEvents:FireClient(player, "Ah, I have a headache... I can't see...")
end)
--Local script, located in the gui
game.ReplicatedStorage.ChatEvents.OnClientEvent:Connect(function(sentence)
	--The rest of your code here
end)

Let me know if you have any questions!

EDIT: This is assuming the Gui you are tweening is a ScreenGui and not a SurfaceGui or BillboardGui, in which case you should use a server script as you were doing before.

This discussion is preserved as it was. New answers, comments, and votes are closed.