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

Removing team player kills when they leave?

blueKills = 0
redKills = 0
for _, player in pairs(game.Players:GetChildren()) do
	local kills = player:WaitForChild("GameFolder").Kills.Value
	if player.TeamColor == game.Teams.Red.TeamColor then
		redKills = redKills + kills
		redValue.Value = redKills
	elseif player.TeamColor == game.Teams.Blue.TeamColor then
		blueKills = blueKills + kills
		blueValue.Value = blueKills
	end
end	

Can anybody help with this deathmatch script. I have it set so whenever a player kills another player, it gives them a 'kill' Now this script checks for the amounts of kills each player on the team has and adds it to their score. However, if a player with say 4 kills leaves the game, it removes their 4 kills from the games points. How can I stop this from happening?

1 answer

I'm assuming that you're calling this loop every few seconds or something? It would be smarter to connect it to an event.

  • I'm assuming you want your teams to have kill values equal to the sum of all the kills on your team, so your first code should be this:
for _, player in pairs(game.Players:GetChildren()) do
	local kills = player:WaitForChild("GameFolder").Kills -- You want the object, not the value, to connect it to an event
	kills.Changed:Connect(function() -- when their kills increase, add it to the team
		if player.TeamColor == game.Teams.Red.TeamColor then
		        redValue.Value = redValue.Value + 1
		elseif player.TeamColor == game.Teams.Blue.TeamColor then
		        blueValue.Value = blueValue.Value + 1
		end
	end)
end 

now, when they leave, you want to remove that:

game.Players.PlayerRemoving:Connect(function(player)
 	 local kills = player:WaitForChild("GameFolder").Kills.Value -- In this case, you want the value
	 if player.TeamColor == game.Teams.Red.TeamColor then
	        redValue.Value = redValue.Value - kills
	 elseif player.TeamColor == game.Teams.Blue.TeamColor then
	        blueValue.Value = blueValue.Value - kills
	 end
end)

I just made this here, so I may have made a spelling issue or the indentation might be off, but that's how you should be doing it. Hope I helped!

4 comments

0

Please accept my answer if it helped, or ask any questions.

0

Doesn't work properly. Firstly, I dont want to remove the kills when a player leaves, the script I have already does that. The problem with using kills.Changed is that when I kill someone it gives the team like 50 kills, and I cant use a debounce because then if someone gets a kill .5 seconds later it wont be counted

0

why would it give 50 kills? The event is only fired once per change of property, and you're only going to be changing the kills value when they kill someone, +1 each time.

0

Not sure what your question is. If you want their kills to count even after they leave, then just don't include that second code I put. As for the connect thing, it should only be fired once unless you're doing something wrong.

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