Hello! I am trying to make a car spawning script, but
It gives me this error:
game.ReplicatedStorage.Remotes.SpawnCar.OnServerEvent:Connect(function(plr, car)
local pscripts = plr.PlayerScripts
if pscripts.SpawnedCar.Value == true then
plr:Kick("Exploit Detected.")
end
if car.Name == "PorscheButton" then
local scar = game.ServerStorage.Cars.Porsche:Clone()
local rootpart = plr.Character.HumanoidRootPart
scar.Parent = workspace
scar:SetPrimaryPartCFrame(rootpart.CFrame)
scar.Name = plr.Name.. "sCar"
plr.PlayerScripts.SpawnedCar.Value = true
end
end)
that’s the serverscript
and this is the localscript:
script.Parent.MouseButton1Click:Connect(function()
if game.Players.LocalPlayer.PlayerScripts.SpawnedCar.Value == false then
game.ReplicatedStorage.Remotes.SpawnCar:FireServer(script.Parent)
end
end)
Oh. My bad!
I thought it would work because i thought that in the server’s pov it has access to every single game file.
i’ll try to change the SpawnedCar booleanvalue’s parent to the player’s character.
While the main copy of the script exists in StarterPlayerScripts, the running copy only exists in the client context in the PlayerScripts folder.
I would recommend using an array to store which car each player owns, for example:
local playerVehicles = {}
game.ReplicatedStorage.Remotes.SpawnCar.OnServerEvent:Connect(function(plr, car)
if playerVehicles[plr.Name] ~= nil then
plr:Kick("Exploit Detected.")
end
if car.Name == "PorscheButton" then
local scar = game.ServerStorage.Cars.Porsche:Clone()
local rootpart = plr.Character.HumanoidRootPart
scar.Parent = workspace
scar:SetPrimaryPartCFrame(rootpart.CFrame)
scar.Name = plr.Name.. "sCar"
playerVehicles[plr.Name] = scar
end
end)
And as a bonus… cleanup on leave:
game.Players.PlayerRemoving:Connect(function(plr)
if playerVehicles[plr.Name] ~= nil then
playerVehicles[plr.Name]:Destroy()
playerVehicles[plr.Name] = nil
end
end