Why do i have these problems with this Server Script?

So i have a few problems here:

1.) When a player1 or player2 wins player1 is always shown last in the table(when i test it in 2 player mode)
2.) I try to take the HotPotato away from the previous owner but it says its locked(alternative suggestions are appreciated.
3.) Ill provide more problems as the prioritized ones get answered first

Its alot of code so brace yourselfs.

ServerScript:

local function StartRound()
	HotPotatoHolder = nil -- Hot potato holder to nil

	if #PlayersInRound <= 1 then --Checks to make sure it start without having more than 2 players
		print("has less than 2 players")
		coroutine.wrap(function()
			for _, v in pairs(Players:GetPlayers()) do
				v.Character:MoveTo(LobbySpawn.Position)
				v:FindFirstChild("InMatch").Value = false
			end
		end)()

		table.clear(PlayersInRound)
		matchoccuring = false
		StartRound()

	end

	matchoccuring = true

	InfoLabel.Text = "Players Remaining: "..#PlayersInRound

	local timeleft

	--Picking a random holder
	HotPotatoHolder = PlayersInRound[math.random(1, #PlayersInRound)] --Picks Random player to start off with

	--{Cloning Visual Effects & Tool}--
	if HotPotatoHolder then
		local HotPotato = game.ReplicatedStorage.HotPotato:Clone() --Clones Hot potato to parent to chosen one
		HotPotato.Parent = HotPotatoHolder.Character

		local Highlight = game.ReplicatedStorage.Highlight:Clone() --Clones Highlight to parent to chosen one
		Highlight.Parent = HotPotatoHolder.Character

		--{Touched Function}--
		local char = HotPotatoHolder.Character
		local canpass = true

		coroutine.wrap(function()
			for _, v in ipairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					v.Touched:Connect(function(hit)
						if hit:IsA("BasePart") and canpass then
							local foundplr = game.Players:GetPlayerFromCharacter(hit.Parent)
							if foundplr and foundplr ~= game.Players:GetPlayerFromCharacter(char) then
								canpass = false
								HotPotato.Parent = foundplr.Character
								Highlight.Parent = foundplr.Character
								HotPotatoHolder = foundplr
								print(foundplr.Name)
								task.wait(0.5)
								canpass = true

							end
						end
					end)
				end
			end
		end)()

		for i = 10, 0, -1 do --Counting down time with hot potato
			timeleft = i
			CountdownLabel.Text = "("..timeleft..")"
			task.wait(1)
		end

		--{Killing the holder & removing from table when time's up}--
		HotPotato:Destroy()
		Highlight:Destroy()

		HotPotatoHolder.Character:WaitForChild("Humanoid"):TakeDamage(999) --Kills current holder
		HotPotatoHolder:WaitForChild("InMatch").Value = false --Sets their "InMatch" Bool to false
		table.remove(PlayersInRound, PlayersInRound[HotPotatoHolder]) --Removes player from table

	else
		StartRound()
	end
	
	if #PlayersInRound > 1 then --If players is more than 1 then they restart the funtion
		task.wait(0.75)
		StartRound()

	elseif #PlayersInRound <= 1 then --if its = or < then 1 then someone won
		InfoLabel.Text = PlayersInRound[1].Name.." won the Round!!"
		CountdownLabel.Text = "(...)"

		task.wait(2.5)

		for _, plr in pairs(Players:GetPlayers()) do
			plr.Character:MoveTo(LobbySpawn.Position)
			plr:WaitForChild("InMatch").Value = false
		end

		table.clear(PlayersInRound)
		matchoccuring = false
	end 
end

local timeleft = 60
local Players = {}
local State = 'Begin'

while true
       task.wait(1)
       timeleft -= 1
       if timeleft < 0 then -- when game end
            State = 'End'
       end

       if #Players == 1 then  -- when game end
            State =  'End'
       end

       if State == 'Begin' then  -- Game State Logic
            Players = {}
            for _, player in ipaire(game.Players:GetPlayers()) do
                  table.insert(Players, player)
            end
            timeleft = 60
       elseif State == 'End' then
            print('Game End')
       end
end
1 Like
  1. Use table.insert(table, index, value). The 1 in the function is the index you want the item in. If left nil, the function will add that value at the end of the table.
  2. This might happen when an item is Destroyed, but some other code tries to change its parent. When connecting the functions to v.Touched, consider storing what it returns (an RBXScriptConnection) into a separate table. Once the round ends and before removing the HotPotato and Highlight, loop through that connections table and for each item, run v:Disconnect() to make sure the character’s parts no longer fire that function upon touching something.
  3. Okay.

EDIT: Fixed table.insert ordering of parameters

1 Like

I used Table.Insert in a for pairs loop at the beginning I just didn’t show it here

I’ll see if I can implement this

How do i do number 2 Im lost. I tried disconnecting the parts that were fired upon, but i see youve said RBXScriptConnection, how do i insert that?

Make a new table to stored those connections in. Disconnect them before removing the HotPotato and after the round ends.

But have your tried using the second parameter to insert the item to the first index, and placing your value to the third index?

Can you show me an example im confused by your explanation

local Connections : {RBXScriptConnection} = {}

for _, V in SomeTable do
	table.insert(Connections, V.Touched:Connect(function(Hit)
		-- any code
	end)
end

-- round code or whatever
--
--
-- when round ends

for _, V in Connections do
	V:Disconnect()
end

1 Like

Ok this worked, but for the touched fuction as a whole it fires(I know this because it prints) but it only tranfers the potato once(or twice sometimes) Even though when i print what it touches it prints endlessly

Use a debounce variable for the function.

1 Like

I did use a debounce and also with the after using your solution it only works once

Is your debounce happening within the coroutine Touched function?


			coroutine.wrap(function()
				for _, v in pairs(char:GetChildren()) do
					if v:IsA("BasePart") and v.Parent:FindFirstChild("Humanoid") then
						v.Touched:Connect(function(hit)
							if canpass then
								local foundplr = game.Players:GetPlayerFromCharacter(hit.Parent)
								if foundplr and foundplr.Character ~= char then
									if foundplr.Character:WaitForChild("Humanoid").Health ~= 0 then
										canpass = false
										HotPotatoHolder = foundplr

										HotPotato.Parent = HotPotatoHolder.Character
										Highlight.Parent = HotPotatoHolder.Character
										print(foundplr.Name)
										task.wait(0.8)
										canpass = true
									end
								end
							end
						end)
					end
				end
			end)()