How to fix this round-based system?

Hello Developers,

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)

So its like a step to enter round game
image

The script block and the gui are in an model
image

How do I fix this?

Does anyone know how to fix this?

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!

2 Likes

whats the error i mean i should not teleport all players right so uh

I dont think I understand, could you explain whats not working now?

Sorry I was asking whats wrong with my script that it did not work

Could you tell whats the error or mistake in my script so I can improve my scripting skill

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

1 Like

hmm all are small mistakes I guess, anyway thank you for your time and help :wink:

1 Like

image
Hey can you tell why this is an error?

Script

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

But it says line 18 but the TouchEnded function is on line 35-40

and this is insane
image

I did not know this can happen xd

It may say that because the variable is changed in the TouchEnded event, but it is indexed (used) next on line 18

Thats why I removed the currentplayers variable from my code, it’s easy to wrongly separate those 2 numbers if they’re separate variables