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

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

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):

local roundStart = game.ReplicatedStorage.remoteEvents.round.roundStart
local roundEnd = game.ReplicatedStorage.remoteEvents.round.roundEnd

local replicatedStorage = game.ReplicatedStorage
local roleFolder = game.ReplicatedStorage.remoteEvents.roles

local murdererChosen = game.ReplicatedStorage.remoteEvents.roles.murderer
local sheriffChosen = game.ReplicatedStorage.remoteEvents.roles.sheriff
local innocentChosen = game.ReplicatedStorage.remoteEvents.roles.innocent
local neutral = game.ReplicatedStorage.remoteEvents.roles.neutral

local randomRole = math.random(1, 3)
local theChosenRole = game.ReplicatedStorage.TheChosenRole


roundStart.OnClientEvent:Connect(function() 
	
	if randomRole == 3 then
		--code here
		murdererChosen:FireServer(randomRole)
		theChosenRole.Value = "MURDERER"
	elseif randomRole == 2 then
		--code here
		sheriffChosen:FireServer(randomRole)
		theChosenRole.Value = "SHERIFF"
	elseif randomRole == 1 then
		--CODE HERE
		innocentChosen:FireServer(randomRole)
		theChosenRole.Value = "INNOCENT"
	end
end)

roundEnd.OnClientEvent:Connect(function()
	--code here
	neutral:FireServer(randomRole)
	theChosenRole.Value = "NEUTRAL"
end)

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

local chosenNumber = game.ReplicatedStorage.chosenNumber
local status = script.Parent.TextLabel
local tof = game.ReplicatedStorage:WaitForChild("displayChosenTOF")
local chosenRole = game.ReplicatedStorage:WaitForChild("TheChosenRole")
local inround = game.ReplicatedStorage.InRound

local murdererChosen = game.ReplicatedStorage.remoteEvents.roles.murderer
local sheriffChosen = game.ReplicatedStorage.remoteEvents.roles.sheriff
local innocentChosen = game.ReplicatedStorage.remoteEvents.roles.innocent
local neutral = game.ReplicatedStorage.remoteEvents.roles.neutral

local waittime = 5

local roundStart = game.ReplicatedStorage.remoteEvents.round.roundStart
local roundEnd = game.ReplicatedStorage.remoteEvents.round.roundEnd

	innocentChosen.OnServerEvent:Connect(function(player, randomRole)
		status.TextColor3 = Color3.new(3/255, 255/255, 19/255)
	status.Text = "INNOCENT"
end)
	
	murdererChosen.OnServerEvent:Connect(function(player, randomRole)
		status.TextColor3 = Color3.new(255/255, 0/255, 4/255)
	status.Text = "MURDERER"
	end)
	
	sheriffChosen.OnServerEvent:Connect(function(player, randomRole)
		status.TextColor3 = Color3.new(16/255, 40/255, 255/255)
	status.Text = "SHERIFF"
	end)

	neutral.OnServerEvent:Connect(function(player, randomRole)
		status.TextColor3 = Color3.new(255/255, 255/255, 255/255)
	status.Text = "IN LOBBY"
	
end)	

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

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:
local ran = false

roundStart.OnClientEvent:Connect(function() 
    if ran then
        return end
    else
        ran = true
    end

    if randomRole == 3 then
        --code here
        murdererChosen:FireServer(randomRole)
        theChosenRole.Value = "MURDERER"
    elseif randomRole == 2 then
        --code here
        sheriffChosen:FireServer(randomRole)
        theChosenRole.Value = "SHERIFF"
    elseif randomRole == 1 then
        --CODE HERE
        innocentChosen:FireServer(randomRole)
        theChosenRole.Value = "INNOCENT"
    end
end)
  1. 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!

1 comment

0

doesnt work.

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