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

Why is my Remote Event firing more than once?

Asked by
PastDays 108
6 years ago
Edited 6 years ago

So i am making a Debug for my game so i can easily test things, I have a simple GUI with a button to add food, here is the script:

1script.Parent.MouseButton1Click:connect(function()
2    game.ReplicatedStorage.Debug_Events.AddFood:FireServer()
3    print("Client - Debug: ", script.Parent.Parent.Name, " - Fired")
4end)

When clicking the button it should fire the server so that my server script can pick it up and change a value, Here is the script that picks it up:

1while true do wait()
2    local Event = game.ReplicatedStorage.Debug_Events
3    Event.AddFood.OnServerEvent:Connect(function()
4        workspace.Player_Cache.Food.Value = workspace.Player_Cache.Food.Value + 1
5        print("Client - Debug was used to add Food")
6    end)
7end

All i want is for it to fire once and add 1 food to the current amount of food, Instead it fires anywhere from 50 - 900 times, I added a Debounce on both and it still did the same, any help is much appreciated.

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

The 'while true do' part is not needed. This will create a loop, causing it to add your food more than once. The client is not firing the event many times, rather the server is acting upon the event many times.

Try this:

1local Event = game.ReplicatedStorage.Debug_Events
2Event.AddFood.OnServerEvent:Connect(function()
3    workspace.Player_Cache.Food.Value = workspace.Player_Cache.Food.Value + 1
4    print("Client - Debug was used to add Food")
5end)
0
I thought "While true do" was needed for it to notice it being fired, Thank you, it works as expected! PastDays 108 — 6y
1
The server will notice it being fired from the 'OnServerEvent:Connect' part. Glad I could be of help! ThePolite 26 — 6y
Ad

Answer this question