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

How to run a code when a remote event is run, only once????

Asked by 4 years ago

Soooooo, i was trying to do something, and one thing i noticed is that when an event is run, its constantly run until another event is run, i dont get it. my code:

RoleHandler (local script, located in starterplayerscripts):

01local roundStart = game.ReplicatedStorage.remoteEvents.round.roundStart
02local roundEnd = game.ReplicatedStorage.remoteEvents.round.roundEnd
03 
04local replicatedStorage = game.ReplicatedStorage
05local roleFolder = game.ReplicatedStorage.remoteEvents.roles
06 
07local murdererChosen = game.ReplicatedStorage.remoteEvents.roles.murderer
08local sheriffChosen = game.ReplicatedStorage.remoteEvents.roles.sheriff
09local innocentChosen = game.ReplicatedStorage.remoteEvents.roles.innocent
10local neutral = game.ReplicatedStorage.remoteEvents.roles.neutral
11 
12local randomRole = math.random(1, 3)
13local theChosenRole = game.ReplicatedStorage.TheChosenRole
14 
15 
View all 37 lines...

Display Role(server script, located in a GUI that is in starter GUI)

01local chosenNumber = game.ReplicatedStorage.chosenNumber
02local status = script.Parent.TextLabel
03local tof = game.ReplicatedStorage:WaitForChild("displayChosenTOF")
04local chosenRole = game.ReplicatedStorage:WaitForChild("TheChosenRole")
05local inround = game.ReplicatedStorage.InRound
06 
07local murdererChosen = game.ReplicatedStorage.remoteEvents.roles.murderer
08local sheriffChosen = game.ReplicatedStorage.remoteEvents.roles.sheriff
09local innocentChosen = game.ReplicatedStorage.remoteEvents.roles.innocent
10local neutral = game.ReplicatedStorage.remoteEvents.roles.neutral
11 
12local waittime = 5
13 
14local roundStart = game.ReplicatedStorage.remoteEvents.round.roundStart
15local roundEnd = game.ReplicatedStorage.remoteEvents.round.roundEnd
View all 36 lines...

Also, I didn't put a code to this, but I was trying to make it so that it appears for 5 seconds and it disappears, that works, but when it disappears it appears once again and continuously does that until a new event is run.

btw the --code here comments are just because I was doing something before that and marked that there.

1 answer

Log in to vote
1
Answered by 4 years ago

There's two things to take into account here:

1) The function fires more than once presumably because you don't have a built in debounce function to check if the function already ran before. Try this:

01local ran = false
02 
03roundStart.OnClientEvent:Connect(function()
04    if ran then
05        return end
06    else
07        ran = true
08    end
09 
10    if randomRole == 3 then
11        --code here
12        murdererChosen:FireServer(randomRole)
13        theChosenRole.Value = "MURDERER"
14    elseif randomRole == 2 then
15        --code here
View all 23 lines...

2) You don't wanna set a player's GUI through a serverscript, that will generate unnecessary traffic and I recommend you do it in the local script instead for better ping.

Let me know if it worked!

0
doesnt work. VikkiVuk 74 — 4y
Ad

Answer this question