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

How to fix OnClientEvent function firing multiple times for every player that is in the game?

Asked by 5 years ago

I have used a server script to FireAllClients in a local script in order to change gui text for every player to see a certain message. The problem is this: if there are multiple people in-game it fires that remote event multiple times. Say there's 3 people in game, the remote event fires 3 times and makes the OnClientEvent function in the local script fire 3 times in a row for each client. So if I have a message in the gui text, and then change it to say something else say 3 seconds later; the text overlaps itself because the same OnClientEvent function is being fired multiple times for each client based on how many players are in-game.

How can I make it stop firing multiple times for each client, and still have a message shown for the entire server to see?

2 answers

Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago

Hello! To start, :FireAllClients will fire once per person in the game. From there we can bring your error down to the script that fires the clients. Remember, that if you do :FireAllClients, you only have to call it once, not for each player. If you know you are only calling it once, please re-post here with the actual code so I help you more in depth.

0
I posted my code down below for more context, feel free to look! Consecrate_d 0 — 5y
0
Taking a look now! mc3334 649 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Hi! Okay, so I only fired it once but it seems to run that same event for the same player multiple times very quickly; for every player. If there's 3 people in the server and say I'm wanting to make a piece of audio that says "hello," play, then it'd sound something like this "h-h-hello." The audio/event would stutter.

Here's my code for the server script:

game.Players.PlayerAdded:Wait()
wait(30)

remote:FireAllClients("event fired")

Here's my code for the local script:

remote.OnClientEvent:Connect(function(arg)
wait(1)
print(arg)

for i = 5,0,-1 do
wait(1)

plr.PlayerGui.ScreenGui.TextLabel.Text = "whatever" .. tostring(i)

end

music:Play()
plr.PlayerGui.ScreenGui.TextLabel.Text = "whatever2"

wait(5)

plr.Character.Humanoid.Walkspeed = 20
plr.PlayerGui.ScreenGui.TextLabel.Text = "whatever3"

wait(5)

for i = 5,0,-1 do
wait(1)

plr.PlayerGui.ScreenGui.TextLabel.Text = tostring(i)

end

wait(1)

happymusic:Stop()
plr.PlayerGui.ScreenGui.TextLabel.Text = ""
plr.Character.Humanoid.WalkSpeed = 16

remote2:FireServer("server event fired")

end)

Hope that helps gives some more context for the problem, thank you!

0
What prints if anything? mc3334 649 — 5y

Answer this question