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

Team doesn't get changed on death?

Hi, in my game when you die, your team gets changed, but it doesn't work, i don't know what's wrong with the script. This is in a normal script

local teams = game:GetService("Teams")

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
		
			print(player.Name .. " has died!")
			player.Team = teams:FindFirstChild("Lobby")
			print('fire')
			
			if player.Team == teams:FindFirstChild("Juggernaut") then
				game.ReplicatedStorage.Values.JuggernautPlayers.Value = game.ReplicatedStorage.Values.JuggernautPlayers.Value-1
			elseif player.Team == teams:FindFirstChild("Sheriff") then
				game.ReplicatedStorage.Values.SheriffPlayers.Value = game.ReplicatedStorage.Values.SheriffPlayers.Value-1
			end
		end)
	end)
end)

2 comments

0

Both prints are there, but team doesn't change.

0

Fix your indentation please. Perhaps add more print that would to see what if statement is evaluated to true (if any). Other than that, check output for errors.

1 answer

✓ Accepted answer

The problem

There issue in your code is rather simple. On line 8 you set the players new team to the lobby. But in later code you compare the players team again. As you have set the players team to the lobby none of the comparisons will be true.

player.Team = teams:FindFirstChild("Lobby")
...
if player.Team == teams:FindFirstChild("Juggernaut") then
...

You simply need to do this logic before assigning the player a new team. In addition to this you should be creating more variables as it will simplify the code a lot and help improve performance (you no longer look for the team each time)

-- create variables of teams
local teams = game:GetService("Teams")
local juggernaut = teams.Juggernaut
local sheriff = teams.Sheriff
local lobby = teams.Lobby

-- also make variables of your value holders
local repStorage = game:GetService("ReplicatedStorage")
local juggernautPlayers = repStorage.Values.JuggernautPlayers
local sheriffPlayers = repStorage.Values.SheriffPlayers

game:GetService("Players").PlayerAdded:Connect(functio(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")
		
			-- dec team values
			if player.Team == juggernaut then
				juggernautPlayers.Value = juggernautPlayers.Value - 1
			elseif player.Team == sheriff then
				sheriffPlayers.Value = sheriffPlayers.Value - 1
			end
			
			-- set player team after 
			player.Team = lobby
		end)
	end)
end)

Notes

I am not sure why you need to keep a player count for each team as there are events that you can use.

These events should also fire in a local script and may help you simplify your game design.

I hope this helps. please comment if you have any other questions.

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