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!