Struggling with Remote Events

Hello! I’m trying to make a script that deletes a player’s car when a player disconnects. I tried creating a localscript first and it works:

game.Players.PlayerRemoving:Connect(function(player)
    local NomeVeicolo = "Auto di "..player.Name -- player's Car
    local Veicolo = workspace:FindFirstChild(NomeVeicolo)
    if workspace:GetChildren(Veicolo) then
        wait(0.3)
        Veicolo:Destroy()
    end
end)

This localscript (in StarterPlayerScript) works fine, the car actually disappears on the other player’s view but when the disconnected player joins back he can see the car while the other still can’t.

That’s why I thought to use Remote Events, however I’m struggling with them:

LocalScript (In StarterPlayerScript)

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:FindFirstChild("DespawnAuto")

game:GetService("Players").PlayerRemoving:Connect(function(player)
	remoteEvent:FireServer(player, "Despawn")
end)

Server Script (ServerStorage)

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:FindFirstChild("DespawnAuto")

remoteEvent.OnServerEvent:connect(function(player, tipo)
	if tipo == "Despawn" then
		local NomeVeicolo = "Auto di "..player.Name
		local Veicolo = workspace:FindFirstChild(NomeVeicolo)
		if workspace:GetChildren(Veicolo) then
			wait(0.3)
			Veicolo:Destroy()
		end
	end
end)

Does anyone have a solution? Thanks in advance!

you don’t even need to use remote events, you can just check when a player disconnects and remove his car on server side!

1 Like

Tried already, I’ve put the same script in ServerScriptService but didn’t work for some reasons, did also all the changes in that case.

Did you use a local script or a server script.

I used a server script and put it in ServerScriptServ.

Well you really don’t need a remote event for this, if you use the code you had inside of your original local script and paste it inside a server script in ServerScriptService, it should work.

1 Like

I will try again and let you know.

that must mean the cars exist client side and not server sided. can you confirm?

get in your test place, and start the game

spawn a car and then switch to what the server “sees”

go to Test > and press the Current: Client

that should switch to server view, check in the explorer if the car is there

if it is then car is server sided

if not, then it’s client sided and you’re going to have to change some stuff in your code.

1 Like

You do not have to send the player, roblox automatically would do that for a FireServer call. Remove it and just leave the “Despawn” parameter in it. And just leave the code on the server sude the same!

Tried and it works, for some strange reasons when I first tried out this it didn’t work.

1 Like