So I have an obby and I want to add round based challenging stages to play with friends, so its like a round based system now the problem is:
It dosen’t work
No error gets printed or pcall function dosen’t do anything either
Don’t know whats wrong with the script
Here is the script:
local screentext = script.Parent.Parent.Screen.SurfaceGui.TextLabel.Text
local playersinque = {}
local currentplayers = 0
local maxplayers = 4
local id = 000
local tps = game:GetService("TeleportService")
local canwork = true
script.Parent.Touched:Connect(function(hit)
local success,errormessage = pcall(function()
if hit.Parent:FindFirstChild("Humanoid") and canwork == true and currentplayers <= 3 then
canwork = false
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
table.insert(playersinque,player.Name)
currentplayers = currentplayers + 1
wait(0.5)
screentext = tostring(currentplayers).."/"..maxplayers.." players"
wait(5)
if currentplayers >= 2 then
for i,v in pairs(game.Players:GetPlayers()) do
if table.find(v.Name) and (v.Character.HumanoidRootPart.Position - script.Parent.Position).magnitude <= 25 then
tps:Teleport(id,v)
end
end
end
end
end)
if not success then
script.Parent.Parent.Screen.SurfaceGui.TextLabel.Text = errormessage
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and canwork == true then
currentplayers = currentplayers - 1
screentext = tostring(currentplayers).."/4".." players"
end
end)
local screentext = script.Parent.Parent.Screen.SurfaceGui.TextLabel
local playersinque = {}
local maxplayers = 4
local id = 000
local tps = game:GetService("TeleportService")
local players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
local success,errormessage = pcall(function()
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and #playersinque < maxplayers and player and not table.find(playersinque, player) then
table.insert(playersinque,player)
screentext.Text = tostring(#playersinque).."/"..maxplayers.." players"
wait(5)
if #playersinque >= 2 then
for i,v in pairs(playersinque) do
tps:Teleport(id,v)
end
end
end
end)
if not success then
script.Parent.Parent.Screen.SurfaceGui.TextLabel.Text = errormessage
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and player and table.find(playersinque, player) then
table.remove(playersinque, table.find(playersinque, player))
screentext.Text = tostring(#playersinque).."/" .. maxplayers .." players"
end
end)
players.PlayerRemoving:Connect(function(player)
if table.find(playersinque, player) then
table.remove(playersinque, table.find(playersinque, player))
screentext.Text = tostring(#playersinque).."/" .. maxplayers .." players"
end
end)
I edited your script a little and it should work better now, although I’m not 100% sure because the only testing I could do was in a local server
Remember to change the id!
The first mistake i noticed was that you defined screentext as script.Parent.Parent.Screen.SurfaceGui.TextLabel.Text and then tried to change it, which is impossible (you need to use screentext.Text = "some text")
Additionally i removed the currentplayers variable completely and instead replaced it with #playersinque (# returns the amount of things in a table) to avoid problems with the numbers not matching up. This was not a mistake but i prefer it that way
I also removed the canwork variable because it was set to false as soon as the first player touched the teleport, and then never set back to true which means that only 1 person was able to use it. This was probably the mistake that made it not work
Then i just added some code to remove players from the queue if they left the game to make sure it doesn’t break when someone leaves
local screentext = script.Parent.Parent.Screen.SurfaceGui.TextLabel
local playersinque = {}
local currentplayers = 0
local maxplayers = 4
local id = 000
local tps = game:GetService("TeleportService")
script.Parent.Touched:Connect(function(hit)
local success,errormessage = pcall(function()
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and currentplayers <= 3 and not table.find(playersinque,player) then
table.insert(playersinque,player)
local imagelabel = Instance.new("ImageLabel",script.Parent.Parent.Screen.SurfaceGui.Frame)
imagelabel.Name = player.Name
imagelabel.Image = game.Players:GetUserThumbnailAsync(player.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size150x150)
currentplayers = currentplayers + 1
wait(0.25)
screentext.Text = tostring(currentplayers).."/4 Players"
wait(5)
if currentplayers >= 3 then
for i,v in pairs(playersinque) do
if (script.Parent.Position - v.Character.HumanoidRootPart.Position).magnitude <= 50 then
tps:Teleport(id,v)
end
end
else
screentext.Text = "Atleast 3 players needed to start"
wait(2)
screentext.Text = tostring(currentplayers).."/"..maxplayers.." players"
end
end
end)
if not success then
script.Parent.Parent.Screen.SurfaceGui.TextLabel.Text = errormessage
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if table.find(playersinque,player) then
currentplayers = currentplayers - 1
screentext = tostring(currentplayers).."/4".." players"
script.Parent.Parent.Screen.SurfaceGui.Frame:FindFirstChild(player.Name):Destroy()
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if table.find(playersinque,plr) then
table.remove(playersinque,table.find(plr))
script.Parent.Parent.Screen.SurfaceGui.Frame:FindFirstChild(plr.Name):Destroy()
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if table.find(playersinque,player) then
currentplayers = currentplayers - 1
screentext = tostring(currentplayers).."/4".." players" --<<< on this line
script.Parent.Parent.Screen.SurfaceGui.Frame:FindFirstChild(player.Name):Destroy()
end
end
end)
You forgot to put .Text after screentext on the line i highlighted