When the character walks onto a block the following code should trigger. In studio it works, but when published it does not.
local cop = "Cop"
local copCars = {"Car Chassis", "1992 WVD Masamune 3.2 GT"}
local cars = {"'54 Caddy", "2015 BMW 435i", "G500", "KZ Converge", "Q7", "Wrangler Rubicon"}
function spawnVehicle()
local player = game.Players.LocalPlayer
local car = ""
if player.hasVehicle.Value == 1 then
print("Player already has car")
else
print ("player does not have car")
player.hasVehicle.Value = 1
if player.TeamColor == game.Teams[cop].TeamColor then
car = copCars[math.random(1,#copCars)]
else
car = cars[math.random(1, #cars)]
end
print(car.. " was randomly choosen")
local model = game.ServerStorage[car]:clone()
model.Parent = game.Workspace
model:MakeJoints()
model:MoveTo(script.Parent.Parent.Position)
waitForNextCar(player)
end
end
function waitForNextCar(player)
local i = 1
while i < 10 do
wait(1)
i = i + 1
end
player.hasVehicle.Value = 0
end
function onHit(hit)
if hit.Parent:WaitForChild("Humanoid") then
spawnVehicle()
end
end
script.Parent.Touched:connect(onHit)

3 comments
Are you using this as a local script within a part? If not, line 7 is most likely causing the problem as you can not locate a Local Player from a regular script.
Furthermore, if filtering is enabled, this would not work from a local script. To get the player in a server script, you would have to get the player from hit.Parent and supply that to spawnVehicle as a parameter.
Thank that was it changed to game.Players:GetPlayerFromCharacter(hit.Parent). Also to those who care I updated spawnVehicle(hit) to take a passed variable.