Script won't FireServer on Touched

Hello! I have been trying to make a script fire an event whenever someones touches its parent, but, I’m dumb and can’t get the script to fire it. My scripters are away from home right now so I came here for help. If you could figure out why this basic script won’t work, that would be great!

Image:

Script:

local RemoteEvent = game:GetService("ReplicatedStorage").Eat

script.Parent.Touched:Connect(function(Touched)
	if Touched then
		RemoteEvent:FireServer()
		script.Parent:Destroy()
	end
end)

I do see some issues, but in order to fix them I need to know a few things.

What exactly does the RemoteEvent do when fired? Is this scirpt a Script or a LocalScript? Also, the script that receives the event, is it client-sided or server-sided?

Maybe you need to read this OnServerEvent

  1. Local script
  2. The RemoteEvent does the image below when fired
  3. And the script that receives the event is the client

    (Image is the client)

is the script parent a BasePart?

The problem is that LocalScript doesn’t work on workspace as you can see here
so you need to change the script directory

Local scripts are client sided, and don’t run in workspace.

Therefore, you would need the script that sends the event to be a Script, not a LocalScript.

You would also need to revise some other things as well, so these are the revisions I would make:

SCRIPT IN PART (SCRIPT):

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Eat")

script.Parent.Touched:Connect(function(Touched)
    local plr = game.Players:GetPlayerFromCharacter(Touched.Parent)
	if plr then
		RemoteEvent:FireClient(plr)
		script.Parent:Destroy()
	end
end)

SCRIPT IN CLIENT (LOCALSCRIPT):

local Player = game.Players.LocalPlayer
game.ReplicatedStorage:WaitForChild("Eat").OnClientEvent:Connect(function()
    Player.Hunger.Value+=INCREASE_AMOUNT
end)

I believe it’s the

script.Parent:Destroy()

that messes up the signal. It takes a while to send a signal from the client to the server, so you may be removing the script before the signal is fully sent to the server. This happened to me in a sword fighting game I was working on where I needed to remove the part as soon as it was touched, and needed to send a signal to the server from the client.

Instead, you could do something like this:

local CanTouch = true
script.Parent.Touched:Connect(function(Touched)
    if Touched and CanTouch then
        CanTouch = false
        RemoteEvent:FireServer()
        script.Parent.Transparency = 1
        script.Parent.CanCollide = false
        script.Parent.Anchored = true
        wait(1)
        script.Parent:Remove()
    end
end)

You cannot know the exact time :FireServer() has sent.
try sending :FireClient() in the server script too.
Then if you receive onClientEvent,
Then you can remove the script.

Try

local RemoteEvent = game.ReplicatedStorage.Eat

function touched(eat)
	if touched then
		RemoteEvent:FireServer()
		script.Parent:Destroy()
	end
end)

script.Parent.Touched:Connect(touched)

You could also simplify this by using a RemoteFunction instead of a RemoteEvent. It does the same thing, but without the hassle of making more functions for more events.