Player being duplicated(?) issue

Hello! I’ve come across a bug in my system. In the script below, when the player dies their team becomes Waiting.

local player = game:GetService("Players").PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local teams = game:GetService("Teams")
local playerobject = player.TeamColor
local folder = game.Workspace.PlayerIngameFolder
while true do
	humanoid.Died:Connect(function(character)
		if player.Team ~=  teams["Waiting"] then
			player.Team = teams["Waiting"]
		end
	end)
	wait(0.1)
end

In the script below, this script dictates the round depending on the timer.

local winner = script.Parent
local teams = game.Teams
local spawn = workspace.TeleportSpawn
local ground = workspace.TeleportGround
game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local timer = player:WaitForChild("PlayerGui"):WaitForChild("timer"):WaitForChild("timer")
		local teleport = false
		while true do
			if timer.Text == "Intermission.." and not teleport then
				player.TeamColor = BrickColor.new("White")
				if character.Parent ~= workspace.PlayerWaitingFolder then
					character.Parent = workspace.PlayerWaitingFolder
					character.HumanoidRootPart.Position = spawn.Position
				end
				teleport = true
			elseif timer.Text == "Round Starting" and teleport then
				player.TeamColor = BrickColor.new("Lime green")
				if character.Parent ~= workspace.PlayerIngameFolder then
					character.Parent = workspace.PlayerIngameFolder
					character.HumanoidRootPart.Position = ground.Position
				end
				teleport = false
			end
			task.wait(0.1)
		end
	end)
end)

But, when put together, you get something like this when a player dies.
image
It then stacks the more times you die, resulting in an inaccurate number of players when the timer (script below) displays it.

local time = 20
local gui = script.Parent
local folder = workspace.PlayerIngameFolder
gui.Text = time

while true do
	if time ~= 0 then
		time -= 1
		wait(1)
		gui.Text = time
	elseif time == 0 then
		print(#workspace.PlayerIngameFolder:GetChildren())
		gui.Text = "Round Ended! " .. #workspace.PlayerIngameFolder:GetChildren() .. " winners!"
		wait(3)
		gui.Text = "Intermission.."
		wait(3)
		gui.Text = "Round Starting"
		wait(3)
		time = 20
		gui.Text = time
	end
end

Thanks in advance for helping!

3 Likes

Hey!
I just wrote a new script from scratch based on your script and tested it: It’s supposed to work (server-sided script)
I just can’t see the reason why your script was failing. (Cause I’m dumb)
So yeah, if you wanna know the reason why your scripts were failing, might wait for a new answer from someone better at scripting than me.

local teams = game:GetService("Teams")
local time_ = 20
local gui = script.Parent
local folder = workspace.PlayerIngameFolder
local spawn_ = workspace.TeleportSpawn
local ground = workspace.TeleportGround

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local timer = player:WaitForChild("PlayerGui"):WaitForChild("timer"):WaitForChild("timer")
		local teleport = false
		character:WaitForChild("Humanoid").Died:Connect(function()
			player.Team = teams.Waiting
		end)
		timer:GetPropertyChangedSignal("Text"):Connect(function(text)
			if timer.Text == "Intermission.." and not teleport then
				player.TeamColor = BrickColor.new("White")
				if character.Parent ~= workspace.PlayerWaitingFolder then
					character.Parent = workspace.PlayerWaitingFolder
					character.HumanoidRootPart.Position = spawn_.Position
				end
				teleport = true
			elseif timer.Text == "Round Starting" and teleport then
				player.TeamColor = BrickColor.new("Lime green")
				if character.Parent ~= folder then
					character.Parent = folder
					character.HumanoidRootPart.Position = ground.Position
				end
				teleport = false
			end
		end)
	end)
end)

local function setTextForPlayers(text)
	game.StarterGui:WaitForChild("timer"):WaitForChild("timer").Text = text
	for i, plr in ipairs(game.Players:GetPlayers()) do
		local timer = plr:WaitForChild("PlayerGui"):WaitForChild("timer"):WaitForChild("timer")
		timer.Text = text
	end
end

setTextForPlayers(time_)
while true do
	if time_ ~= 0 then
		time_ -= 1
		wait(1)
		setTextForPlayers(time_)
	elseif time_ == 0 then
		print(#folder:GetChildren())
		setTextForPlayers("Round Ended! " .. #folder:GetChildren() .. " winners!")
		wait(3)
		setTextForPlayers("Intermission..")
		wait(3)
		setTextForPlayers("Round Starting")
		wait(3)
		time_ = 20
		setTextForPlayers(time_)
	end
end

Hope this can fix your issue!

I just rushed to write ya something since yeah, you’ve been waiting for 4 hours already x(
The script is also not that optimized, modify it to your liking :)

2 Likes

So I did your code and it works great! Does the same things as my old scripts did (They were also server-sided).

However…
image

same issue.

Do you have any script that makes a clone of the player? Like a dead body script or smth? As yeah, it could justify the problem.

If you do, just implement a line that will put the character in workspace or a dead bodies folder like this (can be added to the last script that I sent):

character:WaitForChild("Humanoid").Died:Connect(function()
	player.Team = teams.Waiting
	character.Parent = workspace --workspace.DeadBodiesFolder --> If ya want a folder for it for example.
end)

Add a debounce to the forever loop in the first script

What’s a debounce even gonna do here?

I do not have a script that makes a clone. Otherwise, I would’ve mentioned that already.

However, I do have a custom character (a unicorn) that the players would spawn as. Is that the issue?

image

Also, when highlighting the two players it gets the current player and the old player’s body parts.
image

I honestly don’t know at this point, I never had to deal with custom chars as I don’t use them.
Implementing a script that deletes the custom character after death would be a simple solution that just works.

--To implement with the first script that I wrote:
character:WaitForChild("Humanoid").Died:Connect(function()
	player.Team = teams.Waiting
	local Event = player.CharacterAdded
	Event:Connect(function()
		character:Destroy()
		Event:Disconnect()
	end)
end)

Sorry that I couldn’t help ya with this one.

there is no debounce in your code you need to first add a debounce to moving the character the Model may duplicate because you are changeing the player’s character Parent really quickly so i belive the Roblox Server thinks the Player’s Character Model has gone missing so it recreates the model. This is because the Character Model is actually Parented to 2 things the Player Instance and the Workspace
so when you access and change the Parent really quick sometimes an error can occur where the Player’s Instance is not properly connected to the character Instance so the Server Refires the CharacterAdded for the PlayerInstance. To avoid add a task.delay into your code before moving the Character Parent. Another thing this is just my personal Opinion but you should not be using While true do loop in this case a Run service heartbeat would be more optimised . Also you should not need to use WaitForChild for the PlayerGui if you do that is A bit weird.

1 Like

This helped! However, two new errors.


Also, how do you learn to script like that?

RBXScriptConnection is what you Disconnect() not Signal
as for your 1st Error it indicates what you are parenting to either does not exist or has not loaded in fast enough

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.