Can't delete part using RemoteEvent and a click detector?

I’m trying to make a click-detector part that will delete a specific block for the entire server. I have tried both RemoteFunction and RemoteEvent to achieve this but both of those say it needs to be client based.

S.S.C SCRIPT: (REGULAR SCRIPT)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local DeletePart = ReplicatedStorage:WaitForChild(“RemoteEvent”)
DeletePart.OnServerEvent:Connect(function(player, part)
part:Destroy()

CLICK.D.SCRIPT: (REGULAR SCRIPT)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local DeletePart = ReplicatedStorage:WaitForChild(“RemoteEvent”)
local part = script.Parent
function onClick(hit)
DeletePart:FireServer(part)
print(“Achieved”)
end
script.Parent.ClickDetector.MouseClick:connect(onClick)

I’ve been trying to fix this for 3 hours with no luck. Sorry if the question is stupid lol.

So the fix: Click.D.Script needs to be local to communicate with ReplicatedStorage remote, and it’s unnecessary to handle an event such as part:Destroy() using remotes.

2 Likes

Basically, the mistake you did is forgetting to add end) in the ssc script

Your code should look like this

Remote.OnServerEvent:Connect (function(Player , Part)
Part:Destroy()
end)

EDIT: Forgot to mention that you need to fire a remote event in a local script not normal script

ssc script (server script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DeletePart = ReplicatedStorage:WaitForChild("RemoteEvent")
DeletePart.OnServerEvent:Connect(function(player, part)
part:Destroy()
end)

clickdscript (local script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DeletePart = ReplicatedStorage:WaitForChild("RemoteEvent")
local part = script.Parent
function onClick(hit)
DeletePart:FireServer(part)
print("Achieved")
end
script.Parent.ClickDetector.MouseClick:connect(onClick)

explatatotion:
fireServer is only for localscripts, if you fire it from the server the server will be confused who the f fired the remote event
script.Parent.ClickDetector.MouseClick:connect(onClick) - localscripts doesn’t work in workspace/serverscriptservice/serverstorage
you’re missing a end) in ssc script

edit: you could change script.Parent.ClickDetector in clickdscript to part.ClickDetector instead

2 Likes

Events of the ClickDetector work/can be activated on both the Client and Server Side – based on the description of what you’re trying to achieve, this could be consolidated into a single server script without the need for LocalScripts or RemoteEvents.

Example:

local Part = script.Parent
local ClickDetector = Part.ClickDetector

ClickDetector.MouseClick:Connect(function()
    part:Destroy()
end)
2 Likes

You’re over-complicating this. You don’t need a RemoteEvent since you’re handling the clicked event on the server anyway.

2 Likes

When should I be using replicated remotes vs regular scripts in the workspace? Say I wanted to make a simple tycoon, couldn’t all of it be handled without using remotes?

RemoteFunctions and RemoteEvents are necessary when the Client needs to communicate something that the server should see or request something from the server that the client is unable to/shouldn’t do on its own.


Example 1

For example, if you created an in-game shop through a ScreenGui and a user clicked a TextButton to purchase an item with their leaderstats/in-game currency, communication with the server would be necessary to ensure the following:

  • That the player has met all the requirements to purchase the item

  • If the purchase is meant to deduct from their leaderstats, doing so on the server ensures that the change is visible to the server and the player (and this prevents players from spoofing their leaderstats on the client side to use more in-game currency than they actually do)

  • If the item they need has been stored somewhere such as the ServerStorage, only the server would be able to access and provide it to the player


Example 2

There are some things that need to be managed via LocalScripts to prevent visual latency/delayed responses from affecting the user experience or for player-specific settings, which are most common when working with the user interface.


For instance, when you step onto the train in Jailbreak, your position is updated through a LocalScript to make sure that your Character is smoothly moving with the train and not teleporting around.

Another example could be an in-game settings menu that allows you to turn on/off in-game music, change keybinds, etc. which could be different depending on the preferences of each player.


Those were some general examples to hopefully provide a better idea of situations that would involve primarily working with LocalScripts vs communication between LocalScripts and Server Scripts. Here are some additional resources I’d recommend for learning more:

Resources

Roblox Client-Server Model - Developer Hub Article

Securing Your Roblox Game - Developer Forum Community Tutorial

Validating User Input - Developer Forum Community Tutorial

2 Likes