I’m new to scripting, and I wanted to test my skills by doing a Tycoon game in which Monkeys collect bananas. (lol) The Monkeys have an animation that drops the banana by cloning a Banana Mesh in ReplicatedFirst and putting it on Workspace.
The problem is that sometimes the script clones two Banana Meshes instead of one at the same time, and I don’t know how it’s happening.
I tried using Time Position, Debounce variables and Animation Events, but all of those gave the same result.
This is the script.
local monkey = script.Parent.Monkey
local animationTrack = monkey.Humanoid:LoadAnimation(monkey.Grab)
local bananaPos = monkey.BananaPos
local Debris = game:GetService("Debris")
animationTrack:Play()
animationTrack:GetMarkerReachedSignal("BananaDrop"):Connect(function()
local banana = game:GetService("ReplicatedFirst").Banana:Clone()
banana.Parent = game.Workspace
banana.Position = bananaPos.Position
banana.Anchored = false
Debris:AddItem(banana)
end)
If someone has a solution I would be very grateful.
This is because your AnimationTrack is constant, meaning you don’t assign animation every time.
When AnimationTrack uses function “GetMarketReachedSignal” it connects to the function that runs your script. And each time they will increase, as they are just being connected. To fix the issue you have to make a variable which will have the value of this whole function and then disable it.
local monkey = script.Parent.Monkey
local animationTrack = monkey.Humanoid:LoadAnimation(monkey.Grab)
local bananaPos = monkey.BananaPos
local Debris = game:GetService("Debris")
local Function = nil
animationTrack:Play()
Function = animationTrack:GetMarkerReachedSignal("BananaDrop"):Connect(function()
Function:Disconnect()
local banana = game:GetService("ReplicatedFirst").Banana:Clone()
banana.Parent = game.Workspace
banana.Position = bananaPos.Position
banana.Anchored = false
Debris:AddItem(banana)
end)
local monkey = script.Parent.Monkey
local animationTrack = monkey.Humanoid:LoadAnimation(monkey.Grab)
local bananaPos = monkey.BananaPos
local Debris = game:GetService("Debris")
animationTrack:Play()
animationTrack:GetMarkerReachedSignal("BananaDrop"):Connect(function()
if not game.Workspace:FindFirstChild("Banana") then
local banana = game:GetService("ReplicatedFirst").Banana:Clone()
banana.Parent = game.Workspace
banana.Position = bananaPos.Position
banana.Anchored = false
Debris:AddItem(banana)
end
end)