Stop Waiting if tool isn't equipped?

Hello,

I am creating a script where if the player is holding a tool, it plays a looping animation. After the animation has finished a single interation - this is checked by waiting reallength seconds - it will change the value of news by newsamount.

However, if the tool is UNEQUIPPED while waiting, it should not change the value of news and reset the wait.

I am confused on where to start. Any help would be greatly appreciated. Here is my source code:

game.Players.PlayerAdded:Connect(function(player)
	local news = player:WaitForChild("leaderstats"):WaitForChild("news")
	player.CharacterAdded:Connect(function(character)
		local read = Instance.new("Animation")
		local humanoid = character:WaitForChild("Humanoid")
		read.Parent = humanoid
		read.AnimationId = "rbxassetid://11827469203"
		repeat task.wait() until character.Parent == workspace
		local readanim = humanoid.Animator:LoadAnimation(read)
		local tool = character:WaitForChild("NewsTool")
		local length = readanim.Length
		local reallength = length
		local equipped = false
		
		--adjust speeds
		local speed = player:WaitForChild("hiddenstats").readspeed
		
		-- adjust amount of news
		local newsamount = player:WaitForChild("hiddenstats").newsamount
		
		tool.Equipped:Connect(function()
			readanim:Play()
			readanim:AdjustSpeed(2)
			equipped = true
		end)
		tool.Unequipped:Connect(function()
			readanim:Stop()
			equipped = false
		end)

		-- insert wait + increase news value script here. You can find previous code (that doesn't really work) here
		--while true do
		--	wait(reallength)
		--	if equipped then
		--		if newsamount.Value == 0 then
		--			newsamount.Value = 1
		--		end
		--		news.Value += newsamount.Value 
		--	end
		--end
	end)	
end)```

what do you mean by this?
are u saying that newsamount increases by one after each time animation is finished?

No, newsamount is the amount news changes by
news.Value += newsamount.Value
Sorry for the late reply.
Newsamount is a datastore value that you can increase with upgrades in my game.

I would make this with RemoteEvent.

That’s… not very helpful…
Could you elaborate?

You should disconnect the connection!

local connection = tool.Equipped:Connect(function()
			readanim:Play()
			readanim:AdjustSpeed(2)
			equipped = true
		end)
		tool.Unequipped:Connect(function()
connection:Disconnect()
			readanim:Stop()
			equipped = false

		end)

Could you explain this to me? What is a connection? What does it mean to disconnect it?

Instead of making “ToolEquipped event-ToolUnequipped event-Player variable- Character variable” in PlayerAdded or CharacterAdded, i would be using :Fireclient and OnServerEvent events.

Are there any advantages to this? This part works…
The only part that I’m concerned about is resetting the cooldown if the tool is unequipped.

the connection in this case was

tool.Equipped:Connect(function()
			readanim:Play()
			readanim:AdjustSpeed(2)
			equipped = true
		end)

A connection is something that makes something happen when an event occurs. So in that example, when the tool is equipped, the animation will play. Disconnecting it means that instead of the animation playing, nothing will happen.

Doesn’t that already happen? Will this solve my problem?
I have NO issue with the animation. All I am concerned about is the timer between changing the value of News.

I am confused by you asking if it happens already. I think it should solve the problem because the code should make it so that when the tool is unequipped, what happens when the tool is equipped stops happening.

What do you want, elaborate. Do you want to increase value of news constantly when equipped and reset value of news when unequipped?

No. Here is what should happen.
If tool is equipped

play animation
wait a few seconds
increase news value by 1

However, if the tool is unequipped WHILE THE SCRIPT IS WAITING

stop animation
DO NOT INCREASE NEWS VALUE BY 1, STOP WAITING

@LolBloxLolxxxx

I see!

local connection = tool.Equipped:Connect(function()
			readanim:Play()
			readanim:AdjustSpeed(2)
			equipped = true
		end)
		tool.Unequipped:Connect(function()
connection:Disconnect()
			readanim:Stop()
			equipped = false

		end)

Then disconnecting should still work as disconnecting the connection stops it from

play animation
wait a few seconds
increase news value by 1

Sorry, I was asleep! I will give this a go now!
Where exactly should I put the news.Value += 1?

OK, I found a new solution which I think is cleaner.
I opted to use :GetMarkerReachedSignal to signify when to increase news.Value. I think it is more efficient AND its not affected by :AdjustSpeed, as well as not needing a timer or anything.
I did end up using your connection system though, and I do quite like it!
Thanks all.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.