How do I make a part respawn when touched?

Tried to mash up some scripts so that when you touch a coin, it gives you x amount of cash, then gets destroyed so you cant get anymore cash from it, then 45 seconds after, it respawns. How would I do this?

If you have a leaderstat already, just connected the touched event to a function to get the player from the touched part. If there is a player update the leaderstat then parent the coin to nil, wait 45 seconds, and parent the coin to workspace again. #help-and-feedback:scripting-support is for help with code, so I’d recommend asking more specific questions and providing code you’re using.

Edits:

To parent something to nil:


local instance = Instance.new(“Part”)
instance.Parent = nil
wait(45)
instance.Parent = game:GetService(“Workspace”)

something like this? ik my code is a bit messy, i tried to label where i think the problem is.

local TweenService = game:GetService(“TweenService”)
local tweenedpart = script.Parent

local Info = TweenInfo.new(
.3,
Enum.EasingStyle.Quad,
Enum.EasingDirection.InOut,
0,
true,
0
)

local goals =
{
Position = Vector3.new(tweenedpart.Position.X,tweenedpart.Position.Y + 3,tweenedpart.Position.Z);

}
local tween = TweenService:Create(tweenedpart, Info, goals)

amnt = 10

local buttonPressed = false

–pay attention to here i guess

function onTouched(part)

if not buttonPressed then

  buttonPressed = true

tween:Play()
tweenedpart.Sound:Play()
for count = 0, 1, .1 do
wait(.03)
tweenedpart.Transparency = tweenedpart.Transparency + .1
end

local h = part.Parent:findFirstChild(“Humanoid”)
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local lstats = thisplr:findFirstChild(“leaderstats”)
if (lstats~=nil) then
local score = lstats:findFirstChild(“Cash”)
if (score~=nil) then
score.Value = score.Value + amnt
end
end
end

  	wait(1)
  	tweenedpart.Parent = nil
  	wait(5)
  	tweenedpart.Parent = workspace.GrasslandCoins
  	tweenedpart.Transparency = 0
  	buttonPressed = true
  end

end
end

script.Parent.Touched:connect(onTouched)


local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local tweenedpart = script.Parent

local Info = TweenInfo.new(
	1, --The time the tween takes
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut,
	0,
	false, --Made it so the tween doesn't reverse
	0
)

local goals = {Position = Vector3.new(tweenedpart.Position.X,tweenedpart.Position.Y + 3,tweenedpart.Position.Z), Transparency = 1;} --Added transparency to this.
local tween = TweenService:Create(tweenedpart, Info, goals)

amnt = 10


local waitTime = 10 --Set to wait time

local debounce = false --Fixed the debounce.
function onTouched(part)
	if debounce then return end

	local humanoid = part.Parent:findFirstChildOfClass("Humanoid")
	if (humanoid~=nil) then
		local thisplr = Players:GetPlayerFromCharacter(part.Parent) --Gets players a better way.
		if (thisplr~=nil) then
			debounce = true
			tween:Play()
			tweenedpart.Sound:Play()
				
			local leaderstats = thisplr:findFirstChild("leaderstats")
			if (leaderstats~=nil) then
				local score = leaderstats:findFirstChild("Cash")
				if (score~=nil) then
					score.Value = score.Value + amnt
				end
			end
			tweenedpart.CanCollide = false
			tween.Completed:Wait() --Waits until the tween is over
			tweenedpart.Parent = nil
			wait(waitTime)
			tweenedpart.Parent = workspace.GrasslandCoins
			tweenedpart.Transparency = 0
			debounce = false
			tweenedpart.CanCollide = true
		end
		
	end
end


script.Parent.Touched:connect(onTouched)

2 Likes