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

On update functions, or a better way of detecting updates?

Hello! I am trying to make a script, in the player's starter gui, which detects their kills, and adds it to their team's score. I need to make sure this only happens when their kills change, because if I don't, it will constantly add to the team's score.

while true do
	wait()
	kos = script.Parent.Parent.leaderstats.KOs.Value
	wait(1)
	if script.Parent.Parent.leaderstats.KOs.Value ~= kos then
		teamc = script.Parent.Parent.TeamColor
		if teamc == BrickColor.new("Bright blue") then
			print("blue")
			game.Teams.Blue.Score = game.Teams.Blue.Score + 			script.Parent.Parent.leaderstats.KOs.Value
	elseif teamc == BrickColor.new("Bright red") then
		print("red")
		game.Teams.Red.Score = game.Teams.Red.Score + script.Parent.Parent.leaderstats.KOs.Value
	elseif teamc == BrickColor.new("Bright green") then
		print("green")
		game.Teams.Green.Score = game.Teams.Green.Score + script.Parent.Parent.leaderstats.KOs.Value
	elseif teamc == BrickColor.new("Bright yellow") then
		print("yellow")
		game.Teams.Yellow.Score = game.Teams.Yellow.Score + script.Parent.Parent.leaderstats.KOs.Value
	end
	end
end

This is what I have, and it doesn't work. The problem seems to be that the KOs value is always equal to their stat, but I want it to be equal to their stat at that time, so I can test their most recent stat to see if it has changed. Thanks in advance :)

4 comments

0

Great, can we see your attempt at using the changed event? This is not a request site.

0

I showed you... I am using it, then testing their team color and updating that team's score. This is not a request

0

Well you gave us no indication what went wrong with your script, and what it sounds like you want us to set up a Changed event function for you.

0

Fine, I'll edit the question.

1 answer

✓ Accepted answer

Used the Changed event on the instance value:

script.Parent.Parent.leaderstats.KOs.Changed:connect(function()
	-- Do stuff
end)

Or, alternatively, you could use a serverside script and just manually record each kill.

ResetEvent = function(Humanoid)
	Humanoid.Died:connect(function()
		if Humanoid:FindFirstChild("creator") then
			--Killer = Humanoid.creator.Value
		end
	end)
end

game.Players.PlayerAdded:connect(function(Player)
	repeat wait() until Player.Character
	
	local Humanoid = Player.Character:FindFirstChild("Humanoid")

	ResetEvent(Humanoid)

	Player.CharacterAdded:connect(function(Character)
		ResetEvent(Character:WaitForChild("Humanoid"))
	end)

end)

This might work... And of course depends on what you're using to detect kills in the first place...

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