Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Arj783 72
4 years ago

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)
0
Use the print() command under each line of code to see if it's firing. Just2Terrify 566 — 4y
0
OK let me try... Arj783 72 — 4y
0
Apparently the "PlayerAdded" function is not working. Arj783 72 — 4y
0
Any reasons for it? Arj783 72 — 4y
View all comments (5 more)
0
The reason that's happening is that the player is loading in before the script does. DeceptiveCaster 3761 — 4y
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 ) Arj783 72 — 4y
0
in this particular case, you cannot use PlayerAdded in a local script Lunaify 66 — 4y
0
also remember you must always do player,sentence instead of just sentence Lunaify 66 — 4y
0
hmm. Thanks. I'll get to it soon! thanks, bye Arj783 72 — 4y

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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?

0
I'll get to it soon, rn im doing another part of my game, thanks tho! Arj783 72 — 4y
0
kk royaltoe 5144 — 4y
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 :) Arj783 72 — 4y
0
Also, question, did roblox remove FireClient ? Arj783 72 — 4y
View all comments (2 more)
0
It's not showing up on my LocalScript Arj783 72 — 4y
0
are you sure your remote is a remote event? bindables and remotes look similar. royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

Answer this question