Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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)
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. Uroxus 350 — 7y
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. tkcmdr 341 — 7y
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. RecoveredVader 0 — 7y

1 answer

Log in to vote
-2
Answered by
Etheroit 178
7 years ago
Edited 7 years ago

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.

Ad

Answer this question