Script wont work if cloned to the workspace

Hello. I’m currently trying to make a part destroy itself whenever it touches something but my script wont work. I’ve tried putting the function call inside an if statement and added print statements to see where it fails but nothing happens.

The weird part about it is that the script only fails if another script has cloned the part it’s in into the workspace. Whenever it put the part into the workspace manually it works just fine.

local Part = script.Parent

local function ontouch()
	print("Part Touched!")
	wait(1.5)
	script.Parent:Destroy()
	print("Part Destroyed!")
end

if script.Parent.Parent == game.Workspace then
	Part.Touched:Connect(ontouch)
end

Are you using a Local Script?

Local Scripts don’t work when it doesn’t have a descendant of the following:

Meaning that if its a descendant of a part in workspace, it won’t run.

1 Like

No, it’s just a normal “Script”. It is cloned into the workspace with a LocalScript though, but i don’t see any way that could affect this script as that works just fine.

@DapperTheGreen
It is cloned into the workspace with a LocalScript though, but i don’t see any way that could affect this script as that works just fine?

Bruh. It does not work like that.
Look at this:

1 Like

If you copy the server script through a local script, then the server script will not work AND LocalScript will only run Luau code if it is a descendant of one of the following objects: Player Backpack, Player Character, PlayerGui, PlayerScripts, ReplicatedFirst

Example:

--localscript
game.ReplicatedStorage.ServerScript:Clone().Parent = game.Workspace --this won't work
game.ReplicatedStorage.LocalScript:Clone().Parent = game.Workspace --this won't work
game.ReplicatedStorage.ServerScript:Clone().Parent = game.Players.LocalPlayer.Character --this won't work
game.ReplicatedStorage.LocalScript:Clone().Parent = game.Players.LocalPlayer.Character --this work
1 Like

Thank you for the information. This seems to have solved the problem. I didn’t think of this previously. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.