Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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.

01while true do
02    wait()
03    kos = script.Parent.Parent.leaderstats.KOs.Value
04    wait(1)
05    if script.Parent.Parent.leaderstats.KOs.Value ~= kos then
06        teamc = script.Parent.Parent.TeamColor
07        if teamc == BrickColor.new("Bright blue") then
08            print("blue")
09            game.Teams.Blue.Score = game.Teams.Blue.Score +             script.Parent.Parent.leaderstats.KOs.Value
10    elseif teamc == BrickColor.new("Bright red") then
11        print("red")
12        game.Teams.Red.Score = game.Teams.Red.Score + script.Parent.Parent.leaderstats.KOs.Value
13    elseif teamc == BrickColor.new("Bright green") then
14        print("green")
15        game.Teams.Green.Score = game.Teams.Green.Score + script.Parent.Parent.leaderstats.KOs.Value
View all 21 lines...

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 :)

0
Great, can we see your attempt at using the changed event? This is not a request site. M39a9am3R 3210 — 9y
0
I showed you... I am using it, then testing their team color and updating that team's score. This is not a request UnityAlex 36 — 9y
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. M39a9am3R 3210 — 9y
0
Fine, I'll edit the question. UnityAlex 36 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Used the Changed event on the instance value:

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

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

01ResetEvent = function(Humanoid)
02    Humanoid.Died:connect(function()
03        if Humanoid:FindFirstChild("creator") then
04            --Killer = Humanoid.creator.Value
05        end
06    end)
07end
08 
09game.Players.PlayerAdded:connect(function(Player)
10    repeat wait() until Player.Character
11 
12    local Humanoid = Player.Character:FindFirstChild("Humanoid")
13 
14    ResetEvent(Humanoid)
15 
16    Player.CharacterAdded:connect(function(Character)
17        ResetEvent(Character:WaitForChild("Humanoid"))
18    end)
19 
20end)

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

Ad

Answer this question