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

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

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

1 comment

0

If you’re listening for a change in value, then just add something thats checks if newvalue >= oldvalue

2 answers

if Moral >10 then
-- Whatever
end

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

1 comment

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

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

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