I am making a part that self destructs when you click it. When I use the destroy function, nothing happens on click.
Edit: Turns out my click detector just doesn’t work and the test from before was an error that made it seem like it worked.
Here’s the code to destroy the part.
Part = game.Workspace.Wedge
game.Workspace.ClickPart.OnServerEvent:Connect(function()
Part:Destroy()
Part = nil
end)
Edit: This is where the scripts are: Game
3 Likes
It is destroying a different part than the part in question. Then subsequent calls results into Attempt to index nil
.
1 Like
Can I see the script where it fires the event?
1 Like
Here. Turns out I must have messed up somewhere in the click detecting since I retested it and it just doesn’t work
Part = game.Workspace.Wedge
--The wedge
ClickDetector = Part.ClickDetector
--Detects click
ClickDetector.MouseClick:Connect(function ()
workspace.ClickPart:FireClient()
end)
1 Like
The mistake that you made there is that your firing the event to the client and not the server. Put the first code in a local script in starter gui; replace OnServerEvent
with OnClientEvent
and rewrite the script with FireClient()
should be written like this:
Part = game.Workspace.Wedge
--The wedge
ClickDetector = Part.ClickDetector
--Detects click
ClickDetector.MouseClick:Connect(function (player)
workspace.ClickPart:FireClient(player)
end)
I added the parameter player
because FireClient()
must be fired to that specific player. FireAllClients()
goes to all clients so it doesn’t need any parameter in it.
2 Likes
They’re different because when I was typing OnClientEvent it only came up with OnServerEvent.
1 Like
That’s what I’m saying that the 1st code has to be in a local script and started in starter gui.
2 Likes
Ohhhhhhhhh it has to be a Local script.
I though it could be a normal script.
1 Like
In starter gui* my bad. (char limit)
2 Likes
I knew it had to be in starter GUI. Why does it have to be in there anyway?
1 Like
By 1st do you mean the one I first posted or the click detection one?
1 Like
There’s a difference: FireServer()
means to fire an event from a client to the server and FireClient()
and FireAllClients()
mean to fire an event from the server to the client or to all clients.
By the way:
Yes, I mean the first posted.
2 Likes
So both of the scripts should be local?
Also all this talk of FireClient() and FireServer() is starting to confuse me a bit. What script should have what type of firing so we can sort this out?
2 Likes
The 1 with fire client has to be regular and the 1 with onClientEvent has to be local.
In the local script in StarterGui:
Part = game.Workspace.Wedge
game.Workspace.ClickPart.OnClientEvent:Connect(function()
Part:Destroy()
Part = nil
end)
In a regular script (Not in StarterGui):
Part = game.Workspace.Wedge
--The wedge
ClickDetector = Part.ClickDetector
--Detects click
ClickDetector.MouseClick:Connect(function (player)
workspace.ClickPart:FireClient(player)
end)
So the regular script doesn’t have to be moved.
1 Like
Ok, thank you. Going to make those edits now and see how it goes. I’ve also noticed that click detectors don’t work in studio when I test. Is that normal?
What I mean is that when I hover over the part in play mode, theres not click detector.
1 Like
That sometimes happens (though it worked for me a while ago), you can also add a proximity prompt to it as well:
Part = game.Workspace.Wedge
--The wedge
ClickDetector = Part.ProximityPrompt
--Detects click
ClickDetector.Triggered:Connect(function (player)
workspace.ClickPart:FireClient(player)
end)
2 Likes
Still not working. I swear Studio hates me. Going to try swapping the code around to see if the code is just in the wrong scripts.
1 Like
You know what, start from the top again.
- Mention what your goal is, explicitly state what and how
- Explain current behavior in contrary to expected
- Leave relevant information, e.g. scripts in action of this behavior
2 Likes
I want to make a part destroy when it’s clicked to release an unanchored sphere since I can’t turn the part into a sphere. Is it possible to change a part’s shape or am I just looking in the wrong places?
1 Like
Let’s try to change this statement a bit more. Can you technically explain the part(first one mentioned)? Also it looks like you’re trying something with physics, e.g. Goldberg machine?
By technically, I mean something like workspace.DeleteThisPart
.
3 Likes