Loop and Value Problem

I want to make a system that gives the player more strength and play an animation and stop when the animation stops. However, when putting the
“plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1” in a loop or more than three times the part where it does some stuff after the animation is stopped doesn’t work. It basically makes actions after it unable to work.

script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
    local stopped = false
	
	local character = plr.Character
	local humanoid = character:FindFirstChild("Humanoid")
	
	plr.Character:PivotTo(script.Parent.Parent.TpPart.CFrame)
	script.Parent.ProximityPrompt.Enabled = false
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://www.roblox.com/asset/?id=16257919280"

	
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
while wait(2) do
	if stopped == true then
		plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1
		end
end
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()


		animationTrack.Stopped:Wait()
		stopped = true
		script.Parent.ProximityPrompt.Enabled = true
		humanoid.WalkSpeed = 16
		humanoid.JumpPower = 50

	
end)
1 Like

That’s because while loops halt code execution, consider running it in another thread.

Something like this:

script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
    local stopped = false
	
	local character = plr.Character
	local humanoid = character:FindFirstChild("Humanoid")
	
	plr.Character:PivotTo(script.Parent.Parent.TpPart.CFrame)
	script.Parent.ProximityPrompt.Enabled = false
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://www.roblox.com/asset/?id=16257919280"

	
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
    task.spawn(function()
        while wait(2) do
            if stopped == true then
	        	plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1
	        end
        end
    end)
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()


		animationTrack.Stopped:Wait()
		stopped = true
		script.Parent.ProximityPrompt.Enabled = true
		humanoid.WalkSpeed = 16
		humanoid.JumpPower = 50

	
end)

Well Im pretty sure the While loop is preventing the script from proceeding beyond that point. Instead of using a while loop, you can try to use the AnimationTrack’s Stopped event to execute code once the animation is finished playing.

This should look something like this:

    local stopped = false --

setting the stopped to false, like you did ofc.

animationTrack.Stopped:Connect(function()
  if not stopped then
      stopped = true
      plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1
      
      script.Parent.ProximityPrompt.Enabled = true
      humanoid.WalkSpeed = 16
      humanoid.JumpPower = 50
  end
end)

If you load the animation in the same thread, and only have to wait for the Stopped event once, you can also use :Once() instead of :Connect() since that would automatically disconnect the event after firing once.

It starts increasing after the animation is over and repeats forever. That’s not exactly what I want to achieve.

I made it like this now and it didn’t work is there another way?

script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
    local stopped = false
	
	local character = plr.Character
	local humanoid = character:FindFirstChild("Humanoid")
	
	plr.Character:PivotTo(script.Parent.Parent.TpPart.CFrame)
	script.Parent.ProximityPrompt.Enabled = false
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://www.roblox.com/asset/?id=16257919280"

	
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
while wait(2) do
	if stopped == true then
		plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1
		end
end
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()
	animationTrack.Stopped:Connect(function()
		if not stopped then
			stopped = true
			plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1

			script.Parent.ProximityPrompt.Enabled = true
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = 50
		end
	end)

	
end)

As said before, the main problem of this script is the While loop, i Tried to adjust The script as good as i can.
I removed the while loop and just use the Stopped event


script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
    local stopped = false
	
    local character = plr.Character
    local humanoid = character:FindFirstChild("Humanoid")
    
    if humanoid then
        plr.Character:SetPrimaryPartCFrame(script.Parent.Parent.TpPart.CFrame)
        script.Parent.ProximityPrompt.Enabled = false
        
        local animation = Instance.new("Animation")
        animation.AnimationId = "http://www.roblox.com/asset/?id=16257919280"
        
        humanoid.WalkSpeed = 0
        humanoid.JumpPower = 0
        
        local animationTrack = humanoid:LoadAnimation(animation)
        animationTrack:Play()
-- this is where you dont need the while loop, just add this behind the first part
        animationTrack.Stopped:Connect(function()
            if not stopped then
                stopped = true
                plr.leaderstats.Strength.Value = plr.leaderstats.Strength.Value + 1
                
                script.Parent.ProximityPrompt.Enabled = true
                humanoid.WalkSpeed = 16
                humanoid.JumpPower = 50
            end
        end)
    end
end)

I didnt test it so there still might be some issues.