Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
D4_rrk 89
4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
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. D4_rrk 89 — 4y
0
I've added a few prints. What does it print? youtubemasterWOW 2741 — 4y
0
Nothing. It prints nothing. D4_rrk 89 — 4y
0
Where is the LocalScript located? (ReplicatedFirst, StarterPlayerScripts, Workspace, etc.) youtubemasterWOW 2741 — 4y
View all comments (8 more)
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. D4_rrk 89 — 4y
0
LocalScript's do not work in the Workspace. They only run in: ReplicatedFirst, StarterCharacterScripts, StarterPlayerScripts, and a player's character. youtubemasterWOW 2741 — 4y
0
Thanks! Now that I know that local scripts don't work in Workspace, I made my project work! D4_rrk 89 — 4y
0
No problem. Please accept my answer. youtubemasterWOW 2741 — 4y
0
How? I'm new to Scripting Helpers. D4_rrk 89 — 4y
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. youtubemasterWOW 2741 — 4y
0
Done! D4_rrk 89 — 4y
0
Thank you. youtubemasterWOW 2741 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)
0
I just changed Destoy() to Destroy() but it still doesn't work. Nothing in the output either. D4_rrk 89 — 4y

Answer this question