Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

RemoteEvent doesn't change the player's team?

Hi, i made a script where it's supposed to change the player's team whenever he dies, but it doesn't work. Can someone help? Main Handler

local function playerDied(player)
	print("Returning "..player.Name.." to lobby.")
	local plr = game.Players:FindFirstChild(player.Name)
	plr.Team = lteam
end
game.ReplicatedStorage.Death.OnServerEvent:Connect(playerDied)

Client Handler

local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
while wait() do
	if hum.Health == 0 then
		game.ReplicatedStorage.Death:FireServer()
	end
end

1 comment

0

try use TeamColor

1 answer

Okay, a few things:

Firstly: local plr = game.Players:FindFirstChild(player.Name) is a bit rhetorical - just use the player variable.

Second: plr.Team = 1team will immediately error because it is a malformed pathway. Access 1team as such >> game.Teams["1team"].

Third: Make sure that BOTH teams are archivable - if they are not, your code will not work.

Fourth: Though using a while loop will not break your script in this instance - it is incredibly inefficient and there is a thousand better ways to handle Humanoid death.

With everything but the fourth point amended - here is your fixed code:

Server:

local function playerDied(plr)
	print("Returning "..plr.Name.." to lobby.")
	plr.Team = game.Teams["1team"]
end

game.ReplicatedStorage.Death.OnServerEvent:Connect(playerDied)

Client:

repeat wait() until game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name) ~= nil
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

while wait() do
	if hum.Health == 0 then
		game.ReplicatedStorage.Death:FireServer()
	end
end

4 comments

0

You can use hum.Died:Connect function

0

@HaveASip My fourth point already made mention of his poor choice to handle Humanoid death with a while loop - but yes, this is a suitable solution.

0

bad bad bad

0

The client one, never loop until something exists, events are better, one could be used. Same for the while loop, use an event.

This discussion is preserved as it was. New answers, comments, and votes are closed.