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

How to destroy a part when a tool is touched, using remote events?

How do I make a part get destroyed when a tool touches it. There is a local script inside the part that I want to be deleted, the code for it is:

script.Parent.Touched:Connect(function(hit)
	game.ReplicatedStorage.OpenMediumDoorEvent:FireServer(hit)
end)

The remote event is inside ReplicatedStorage and the script that will handle the remote is in ServerScriptService, the code for that script is as follows:

game.ReplicatedStorage.OpenMediumDoorEvent.OnServerEvent:Connect(function(hit)
	if hit.Parent.Name == "MediumKey" then
		game.Workspace.Medium.MediumTeleport.PartToBeDeleted:Destoy()
	end
end)

2 answers

✓ Accepted answer

Hello.


Problems:

  • You used Destoy() instead of Destroy().

  • The first parameter of OnServerEvent is always the player who fired that event.


Solutions:

  • Use Destroy() instead of Destoy().

  • Add the "player" parameter to the OnServerEvent event.


Fixed ServerScript:

game.ReplicatedStorage.OpenMediumDoorEvent.OnServerEvent:Connect(function(player, hit)
	print("1")
    if hit.Parent.Name == "MediumKey" then
		print("Deleted")
        game.Workspace.Medium.MediumTeleport.PartToBeDeleted:Destroy()
    end
end)

12 comments

0

Unfortunately, changing Destoy() to Destroy() and adding a "player" parameter on the OnServerEvent script still didn't solve my problem, nothing appeared in Output too.

0

I've added a few prints. What does it print?

0

Nothing. It prints nothing.

0

Where is the LocalScript located? (ReplicatedFirst, StarterPlayerScripts, Workspace, etc.)

0

The local script is located inside a part, which is inside a model with other parts, the model's parent is another model and that model's parent is a folder and that folder's parent is the Workspace.

0

LocalScript's do not work in the Workspace. They only run in: ReplicatedFirst, StarterCharacterScripts, StarterPlayerScripts, and a player's character.

0

Thanks! Now that I know that local scripts don't work in Workspace, I made my project work!

0

No problem. Please accept my answer.

0

How? I'm new to Scripting Helpers.

0

It should be under my post. There's also been a bug where new users can't accept an answer without going into incognito mode. If you don't see the button then do that.

0

Done!

0

Thank you.

On line 3 you wrote Destoy() instead of Destroy() So change your server script to this

game.ReplicatedStorage.OpenMediumDoorEvent.OnServerEvent:Connect(function(hit)
    if hit.Parent.Name == "MediumKey" then
        game.Workspace.Medium.MediumTeleport.PartToBeDeleted:Destroy()
    end
end)

1 comment

0

I just changed Destoy() to Destroy() but it still doesn't work. Nothing in the output either.

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