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

How to improve on AlvinBlox's Car spawning tutorial?

Asked by 3 years ago

Please include the code which you are trying to use, so the community will be better-equipped to help you with your problem.

So I have watched AlvinBlox's tutorial on Car spawning and it works fine but I have one question

how do I make it where a player can only spawn 1 car then his previous car gets deleted?

All the scripts are in his video about it im to lazy to pull up all the scripts and the hierarchy please help

2 answers

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

The general idea is to check if the car exists. There are a few ways you can do this, however, I would recommend doing something like this:

local result, err = pcall(function() -- catches all game-breaking errors
    return car and car.Parent
end)
if result then
    if err == true then
        -- car exists
        car:Destroy()
    else
        -- car does not exist or parent is nil
    end
else
    -- car does not exist or parent is nil
end

-- Car spawn logic
0
and i put this in my leaderstats correct but right above the last end? Pigioty 0 — 3y
1
You did not provide any code, so I am not sure where you would want to place this within your script. I provided a comment at the end which should help with placement. appxritixn 2235 — 3y
0
this is where the car spawns Pigioty 0 — 3y
0
i provided it by awnserwing Pigioty 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited by JesseSong 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).
local function onPlayerJoin(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 22000
    money.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)
    return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
    local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
    car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
    car.Parent = workspace
    car:MakeJoints()
    car.Name = player.Name.."'s "..NameOfCar
end)
0
Fixed Codeblock. JesseSong 3916 — 3y

Answer this question