Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this script work in studio but not when published. Cars don't spawn?

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

0

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.

0

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.

0

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.

1 answer

TO DO

  1. Disable Filtering Enabled (workspace's property),
  2. Place script on serverside and use function from Ad2,
  3. Place code in script and use function from Ad2.

NOTES: Ad1. If Filtering Enabled is on then You cant access clientside on serverside and serverside on clientside Ad2. If script is placed on serverside then You cant use game.Players.LocalPlayer, try game.Players:GetPlayerFromCharacter function instead. Ad3. If script is normal script then You cant use game.Players.LocalPlayer, try changing script to localscript

If have got any more questions then feel free to write comment

PS: If thing work on studio then try first disabling Filtering Enabled and then check cause it may be because it.

This discussion is preserved as it was. New answers, comments, and votes are closed.