Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to prevent item from disappearing until animation is completed?

I made an on touch script so when the tool comes in contact with the object, it disappears. However, it disappears when you just simply walk into it. I'm new at scripting, so just learning!

for example, I pick up the tool and go to chop down a tree, but when I run into the object it disappears and I dont want it to disappear until the tool contacts the tree during the animation....

local tool = script.Parent
local scoop = tool.Scoop

local backpack = tool.Parent
local player = backpack.parent
local playerStats = player:FindFirstChild("leaderstats")
local playerItems = playerStats:FindFirstChild("Items")
local playerSpaces = playerStats:FindFirstChild("Spaces")



local function onTouch(partTouched)
	local canHarvest = partTouched.Parent:FindFirstChild("canHarvest")
	if canHarvest and script.Parent.Animations then
		print("Found an Item") -- for testing purposes.
		if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
			playerItems.Value = playerItems.Value + 1 
			canHarvest.Value = false
			partTouched.Transparency = 1
			partTouched.CanCollide = false
			wait(5)
			canHarvest.Value = true
			partTouched.Transparency = 0
			partTouched.CanCollide = true
			
		end
	end
end

scoop.Touched:Connect(onTouch)

3 comments

0

wait(animation.Length) is what you need!

0

debounce

0

no both of you are wrong its AnimationTrack.Stopped

2 answers

Due to Roblox physics, Touched can be fired multiple times even when the object it's touching is stationary. This is where debouncing comes in.

local tool = script.Parent
local scoop = tool.Scoop

local backpack = tool.Parent
local player = backpack.parent
local playerStats = player:FindFirstChild("leaderstats")
local playerItems = playerStats:FindFirstChild("Items")
local playerSpaces = playerStats:FindFirstChild("Spaces")

local onTouchDB = false -- This is your debounce variable, essentially it's a sign that the action its correlated to has already been/is currently being done

local function onTouch(partTouched)
	if onTouchDB == false then -- Your script checks if the debounce variable is active
	 onTouchDB = true -- Since the action (function) is in progress, the debounce variable is set to true so that the function can't be activated while it's in progress. It's important that this is the first thing after the if statement that checks if it's active
    	local canHarvest = partTouched.Parent:FindFirstChild("canHarvest")
   	 if canHarvest and script.Parent.Animations then
        	print("Found an Item") -- for testing purposes.
        		if canHarvest.Value == true and playerItems.Value < playerSpaces.Value then
            		playerItems.Value = playerItems.Value + 1 
            		canHarvest.Value = false
            		partTouched.Transparency = 1
            		partTouched.CanCollide = false
            		wait(5)
            		canHarvest.Value = true
            		partTouched.Transparency = 0
            		partTouched.CanCollide = true
	 end
        end
    end
	onTouchDB = false -- Since the function has been completed, the debounce variable is set to false so that it can be run again
end

scoop.Touched:Connect(onTouch)

This discussion is preserved as it was. New answers, comments, and votes are closed.