How would I make it where the server will wait until a remote event is finished until something happens.
For example here is some script that I wrote -
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("TimerStart")
script.Parent.Touched:Connect(function(touched)
if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
remoteEvent:FireClient(Player)
wait(1)
end
end
end
end)
When a player touches a part, a remote event is fired. Instead of “wait(1)” how would I make it where instead of waiting a second it makes it where it waits until the remoteEvent is finished and then the player can step on the part again. Thanks.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("TimerStart")
script.Parent.Touched:Connect(function(touched)
if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
remoteEvent:FireClient(Player)
Remote.OnServerEvent:Wait()
wait(1)
end
end
end
end)
@dutycall11 's method wouldn’t work well if another player fires the event first which makes it count. Instead you can fire the event to the player then on the client you can fire the event back when it’s and make a connection to check if it’s the player:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("TimerStart")
script.Parent.Touched:Connect(function(touched)
if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
remoteEvent:FireClient(Player)
local returned = false
local connection
connection = remoteEvent.OnServerEvent:Connect(function(plr)
if plr == Player then returned = true connection:Disconnect() end
end)
repeat task.wait() until returned
end
end
end)
Thanks I get what your saying but I want where a bunch of players will be able to step on the part at the same time. So a bunch of events will be fired all at once. I tried using this code and it makes it where when one player steps on the part the event will be fired for them, but if another player steps on the part nothing happens for that player.
-- Script in a part
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("TimerStart")
script.Parent.Touched:Connect(function(touched)
if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
remoteEvent:FireClient(Player)
local returned = false
local connection
connection = remoteEvent.OnServerEvent:Connect(function(plr)
if plr == Player then returned = true connection:Disconnect() end
end)
repeat task.wait() until returned
end
end
end)
-- local script in StarterPlayerScripts
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("TimerStart")
local function teleport()
local clonedgui = Player.PlayerGui:WaitForChild("Timer")
local minutes = 1
local seconds = 0
repeat
if seconds <= 0 then
minutes = minutes - 1
seconds = 59
else
seconds = seconds - 1
end
if seconds < 10 then
clonedgui.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
else
clonedgui.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
end
Player.Character.Humanoid.Died:Connect(function()
wait(5)
minutes = 1
seconds = 0
end)
wait(1)
until minutes <= 0 and seconds <= 0
end
remoteEvent.OnClientEvent:Connect(teleport)
A timer is cloned for every single player who joins the server. Once a player touches a certain part that timer will start only for the player who touches the part.
Video of problem -
When the player touches the part for the first time the function is fired. But everytime the player touches the part again it keeps firing the function over and over again.
This is because you never added a debounce. You can do this by creating a table that stores the debounce for each player. I just realized I also made a mistake in my previous code where the script doesn’t account for the player leaving the game before the timer is done. New script:
(I also realized you never fired the event back to the server after the timer is done so you should probably do that. Also, It is worth mentioning that handling the timer on the client isn’t the most secure method)
-- Script in a part
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent:RemoteEvent = ReplicatedStorage:WaitForChild("TimerStart")
local Debounces = {}
script.Parent.Touched:Connect(function(touched)
if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player and not Debounces[Player] then
Debounces[Player] = true
remoteEvent:FireClient(Player)
end
end
end)
remoteEvent.OnServerEvent:Connect(function(plr)
Debounces[plr] = nil
end)
Players.PlayerRemoving:Connect(function(plr)
Debounces[plr] = nil
end)
This should do the trick. Tell me if it doesn’t work
Thank you so much, appreciate it. Also you said something about the handling the timer on the client. Right now the timer is in a local script parented to StarterPlayerScripts. Do you mean I should move it somewhere else like into ServerScriptService? Again I appreciate the help, a little new to scripting.