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

How to tell a script to do something only if a value has increased?

Asked by 5 years ago

So in the game I'm making, you're able to take over control points. Take over all of them and a value for your team called "Moral" is increased by 10, and the other team's value is decreased by 5. The problem is I only want to play a song if the value changes by a positive value, but I only know how to play the song if it changes in general. Here's my script below:

wait(1.2)


while true do
    wait(.1)
    local Moral = game.Teams.Prussians.Moral.Value
        local Moral2 = game.Teams.French.Moral.Value

    script.DieWachtAmRhein:Play()
    wait(102)
    script.FrenchAnthem:Play()
    wait(146.5)
    script.PreussensGloria:Play()
    wait(168.5) 


    function ChangedGerman()
      if Moral == Moral + 10 then -- my attempt to only play a new song if
           --it changes by a positive value.

          script.DieWachtAmRhein:Stop()
      script.FrenchAnthem:Stop()
      script.PreussensGloria:Stop()
      game.Workspace.GlobalSoundPlayer.Argonnerwaldlied:Play()

      game.Teams.Prussians.Moral.Changed:Connect(ChangedGerman)
      end
   end
end
0
If you’re listening for a change in value, then just add something thats checks if newvalue >= oldvalue ABK2017 406 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
if Moral >10 then
-- Whatever
end

That's what I'd think it'd be

0
This wouldn't work either since it says if it's greater than ten, but I want it to play when it changes positively. If the team has 20 points and it goes down 5 (thus they lose) the song would still play cause they have 15 points NoahsRebels 99 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

As ABK2017 suggested, you need to create an old value and compare the value with the old one.

--Put these outside the while loop
local OldMoral = game.Teams.Prussians.Moral.Value
local OldMoral2 = game.Teams.French.Moral.Value

while true do
    local Moral = game.Teams.Prussians.Moral.Value
    local Moral2 = game.Teams.French.Moral.Value

    --stuff
    if Moral == OldMoral + 10 then
        OldMoral = Moral
        --Do other stuff
    end
end

Answer this question