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)```
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.
Instead of making “ToolEquipped event-ToolUnequipped event-Player variable- Character variable” in PlayerAdded or CharacterAdded, i would be using :Fireclient and OnServerEvent events.
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.
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.