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

How to extend function wait() if called twice or more?

I've been searching on the internet and I get unrelated stuff so I want clear answers hopefully. The problem is if the remote event was triggered twice, how would I reset the "wait(5)" again so until it the remote stops being called. Then it counts down to 5 until it ends.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	
	player.Time.Value = true
		wait(5)
	player.Time.Value = false
end)

1 comment

0

use a for loop with if statements and probably a debounce unless you are asking for something like delay(5,function()

1 answer

Not quite sure what you're asking, but I've come up with a script.

-- Local Script (inside a tool!)
local Tool = script.Parent
local Player = game.Players.LocalPlayer

local RE = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent",10)
Tool.Activated:Connect(function()
	RE:FireServer()
end)

--[[
#################################################################################
]]

-- Script
local Players = {}

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	local PlrTab = Players[player]
	if(PlrTab ~= nil) then
		local deClick = PlrTab.Clicked
		if(deClick == false) then
			PlrTab.Clicked = true
			print("Clicked!!!, Starting process")
			player.Time.Value = true
			wait(5)
			player.Time.Value = false
			print("Stopping Ignoring Clicks for now!")
			PlrTab.Clicked = false
		else
			print("All ready Clicked ignoring...")
		end
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	--[[ debugging ]]
	local Time = Instance.new("BoolValue",plr)
	Time.Name = "Time"
	Time.Value = false
	--[[ debugging ]] 

	-- Global table hookup
	Players[plr] = {
		Clicked = false
	}
end)
game.Players.PlayerRemoving:Connect(function(plr)
	-- remove the player thats leaving from global table
	if(table.find(Players,plr) ~= nil) then
		Players[plr] = nil
	end
end)

This script logs every player that joins the game and adds your Time bool value to each player. The server side script then waits for a player to activate or click with the tool to change the Time.Value from false to true, as you had, then waits 5 seconds before changing it back to false.

I've made it ignore any other clicks made by a player. Thus stopping the click spam. I've also allow other players to change their Time bool values without locking up the system until the Clicked Value gets changed back to false.

Let me know if you need anything :)

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