I’m trying to make a level system where once u reach the end of one level, u can teleport to the next level from a level select menu, however if 2 people try to click the same level at the same time(or around the same time) one of the players doesn’t get teleported while the other does, whoever clicked earlier. This is my code for what happens when the player clicks a level:
player.CharacterAdded:Connect(function(character)
local hrp = character:WaitForChild("HumanoidRootPart")
playerlevel.OnServerEvent:Connect(function(playero, lev)
if lev then
print(playero)
print(lev)
checkpointclient = FindCheckpoint(playero.Character, lev)
else
return
end
end)
local checkpoint = FindCheckpoint(character, level.Value)
RunService.Heartbeat:Wait()
if checkpointclient ~= nil then
print(player.Name)
print(checkpointclient)
print("checkpointclient")
hrp.CFrame = CFrame.new(checkpointclient.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(checkpointclient.Orientation.Y) + math.rad(0), 0)
checkpointclient = nil
else
print(player.Name)
print(checkpoint)
print("checkpoint")
hrp.CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(checkpoint.Orientation.Y) + math.rad(0), 0)
end
end)
Whenever they click on a level, it resets their character(done in the gui script where they click on the level) so the character added function runs and the gui script also fires an event to the server with the player and level that they chose in it. What should happen is
if checkpointclient ~= nil then
print(player.Name)
print(checkpointclient)
print("checkpointclient")
hrp.CFrame = CFrame.new(checkpointclient.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(checkpointclient.Orientation.Y) + math.rad(0), 0)
checkpointclient = nil
else
this code should run twice because the variable lev(the level that they chose in the gui) exists which allows this if statement
if lev then
print(playero)
print(lev)
checkpointclient = FindCheckpoint(playero.Character, lev)
to run which creates the checkpointclient variable making it not nil, but for some reason it only runs for the first player and then the else statement runs for the 2nd player