Player Script Help

Does anyone know a script that will be a player script that unanchors a part when touched and 10 seconds later it gets deleted? All help is much appreciated :heart:

2 Likes

If you want so, it gotta be a server script.

local part = script.Parent

local function onTouched(hit)
    -- Unanchor the part
    part.Anchored = false
    
    -- Wait for 10 seconds
    wait(10)
    
    -- Delete the part
    part:Destroy()
end

-- Connect the onTouched function to the Touched event
part.Touched:Connect(onTouched)
3 Likes

It doesn’t have to be a server script, but it would only display the effects on the client

2 Likes

mb, if the op want it to be on client side then i guess that’ll work too

2 Likes

One other thing, it’s actually better to use task.wait() compared to wait() since the task library is newer and faster.

2 Likes

Its a single player server. Max size is 1

1 Like

Then it doesn’t matter much if its client or server, it depends on your script choice.

1 Like

The script didn’t work. Did I do something wrong?

1 Like

it’s working for me, did you use local script in the part? or did you put the script anywhere else?

1 Like
--LocalScript inside game.StarterPlayer.StarterCharacterScripts
local character = script.Parent

local function allowedToBeDestroyed(part: BasePart): boolean
	return true
end

local marked = {}
local function touched(hit: BasePart)
	if not allowedToBeDestroyed(hit) then return end
	if table.find(marked, hit) then return end
	table.insert(marked, hit)
	hit.Anchored = false
	task.wait(10)
	--we check in case it fells in void and gets destroyed beforehand
	if hit then hit:Destroy() end
end

for _, child in pairs(character:GetChildren()) do
	if child:IsA("BasePart") then child.Touched:Connect(touched) end
end

character.ChildAdded:Connect(function(child)
	if child:IsA("BasePart") then child.Touched:Connect(touched) end
end)
1 Like

One problem, it unanchors the baseplate and I dont want it to. How can I stop the baseplate from unanchoring?

1 Like

Change

local function allowedToBeDestroyed(part: BasePart): boolean
	return true
end

To

local function allowedToBeDestroyed(part: BasePart): boolean
	if part.Name == "Baseplate" then
		return false
	else
		return true
	end
end
1 Like
local function allowedToBeDestroyed(part: BasePart): boolean
	if part == workspace.Baseplate then return false end
	return true
end