These scripts won't work properly and I can't figure out why

As the title states, I have these two scripts that aren’t functioning correctly. I include the description of the purpose of each script under them, as well as the issue I’m facing with them.

local rep = game:GetService("ReplicatedStorage")
local startEvent1 = rep.FuelCapsuleDeliver
local startEvent2 = rep.CoolantCapsuleDeliver
local eventIn = rep.CooldownOver
local condition1 = rep.FuelCapsuleDeposit
local condition2 = rep.CoolantCapsuleDeposit
local eventOut = rep.TrueCooldownOver
local invFull = 0
local text = script.Parent.TabletGui.Frame.Frame.cooldown

local cooldown = function(inv)
	print("Also Recieved")
	if inv == 0 then
		invFull = 0
	elseif inv == 1 then
		text.Text = "Please deposit capsule before making new request."
	end
end
local thread = coroutine.create(cooldown)

eventIn.Event:Connect(function()
	if invFull == 1 then
		coroutine.resume(thread, 1)
		wait(1)
		coroutine.yield(thread)
	elseif invFull == 0 then
		eventOut:Fire()
		text.Visible = false
	end
end)
condition1.Event:Connect(function()
	print("Recieved")
	coroutine.resume(thread, 0)
end)
condition2.Event:Connect(function()
	coroutine.resume(thread, 0)
end)
startEvent1.Event:Connect(function()
	invFull = 1
end)
startEvent2.Event:Connect(function()
	invFull = 1
end)

The purpose of this script is to check to make sure a capsule hasn’t already been collected before another order for one is made. It works fine the first time the coroutine is called, but the condition1/2 events won’t call it a second time (As evident by the print(“Received”) and print(“Also Received”) lines), so the script wont know I deposited it. However, if the coroutine is called by condition 1/2 first, and then called by the EventIn, it works perfectly fine.

local rep = game:GetService("ReplicatedStorage")

local fCapsule = rep.FuelCapsule
local cCapsule = rep.CoolantCapsule
local fcapAnimControl = fCapsule.AnimationController
local ccapAnimControl = cCapsule.AnimationController

local fDelivEvent = rep.FuelCapsuleDeliver
local cDelivEvent = rep.CoolantCapsuleDeliver

local animID = "rbxassetid://15688301435"
local anim = Instance.new("Animation")
anim.AnimationId = animID

local fRollAnim = fcapAnimControl:LoadAnimation(anim)
local cRollAnim = ccapAnimControl:LoadAnimation(anim)

fDelivEvent.Event:Connect(function()
	fRollAnim.Looped = false
	fCapsule.Parent = workspace
	fRollAnim:Play()
end)
fRollAnim.KeyframeReached:Connect(function()
	print("Stopped")
	fRollAnim:AdjustSpeed(0)
	fRollAnim:Destroy()
	fCapsule:SetPrimaryPartCFrame(CFrame.new(Vector3.new(16.25, 3.5, -25.497)) * CFrame.fromOrientation(math.rad(-47), math.rad(90), 0))
	wait()
	local fRollAnim = fcapAnimControl:LoadAnimation(anim)
end)

cDelivEvent.Event:Connect(function()
	cRollAnim.Looped = false
	cCapsule.Parent = workspace
	cRollAnim:Play()
end)
cRollAnim.KeyframeReached:Connect(function()
	print("Stopped")
	cRollAnim:AdjustSpeed(0)
	cRollAnim:Destroy()
	cCapsule:SetPrimaryPartCFrame(CFrame.new(Vector3.new(16.25, 3.5, -25.497)) * CFrame.fromOrientation(math.rad(-47), math.rad(90), 0))
	wait()
	local cRollAnim = ccapAnimControl:LoadAnimation(anim)
end)

Here is what handles the animation for the capsule to roll down the chute to the player. However, the animation only works the first time, and if I do manage to deposit the capsule in time for the script to work, trying to order a second capsule won’t play the animation.

2 Likes

If you want to reuse your object, why are you deleting it?

1 Like

This was because of another issue where the animation would bring the part to the start of the animation when it ends, changing the animation speed and fade times didn’t work and this was the only solution to get the capsule to stay in place.