Log in to vote
1

Why this CarSpawned script doesn't teleport the car next to the owner?

Asked by 3 days ago

Apparently it works only on R6, but I wanted to make it work on R15, but it spawn the car no where near the owner. Than the car spanwed near the R6 avatar.

So what can I fix this script to make R15 work? Thanks

local P = script.Parent
local player = P.Parent.Parent
local index = 1

local vehicles = game.ServerStorage.Vehicles:GetChildren()


wait(0.5)
-- GUI vars
local bg = P.BG

-- Helper function
function SetInformation(index)
    bg.T.Text = vehicles[index].Name
end

local locked = false
bg.S.MouseButton1Click:connect(function()
    if (locked) then return end
    locked = true
    local vehicleName = player.Name.."Car"
    if game.Workspace:FindFirstChild(player.Name.."Car") then
        game.Workspace[player.Name.."Car"]:Destroy()
    end
    if (not workspace:FindFirstChild(vehicleName)) then
        local model = vehicles[index]:Clone()
        model.Name = vehicleName
        model.Parent = game.Workspace
        model:MakeJoints()
        model:MoveTo(player.Character.Torso.Position + Vector3.new(10, 0, 0))
        bg.Visible = false
    end 
    locked = false
end)

bg.B.MouseButton1Click:connect(function()
    index = index - 1
    if (index < 1) then index = #vehicles end
    SetInformation(index)
end)

bg.N.MouseButton1Click:connect(function()
    index = index + 1
    if (index > #vehicles) then index = 1 end
    SetInformation(index)
end)

SetInformation(index)

1 answer

Log in to vote
0
Answered by
TGazza 974 Moderation Voter
3 days ago

R6 has a Torso as you know but the R15 doesn't have a single Torso bit it has an UpperTorso And a LowerTorso, Because of this your current script will break on R15!.

To fix this, I would reference and use the base/root part of any player/character called HumanoidRootPart

So rather than Calling :

bg.S.MouseButton1Click:connect(function()
    -- code --
    model:MoveTo(player.Character.Torso.Position + Vector3.new(10, 0, 0))
    -- code --
end)

On Line 30 i would change it to:

-- Grab and reference the humanoid root part this is for both R15 and R6!
local HRP = player.Character:WaitForChild("HumanoidRootPart",10)
bg.S.MouseButton1Click:connect(function()
    -- code --
    model:MoveTo(HRP.Position + Vector3.new(10, 0, 0))
end)

Code in full:

local P = script.Parent
local player = P.Parent.Parent
local index = 1
-- Grab and reference the humanoid root part this is for both R15 and R6!
local HRP = player.Character:WaitForChild("HumanoidRootPart",10)

local vehicles = game.ServerStorage.Vehicles:GetChildren()


wait(0.5)
-- GUI vars
local bg = P.BG

-- Helper function
function SetInformation(index)
    bg.T.Text = vehicles[index].Name
end

local locked = false
bg.S.MouseButton1Click:connect(function()
    if (locked) then return end
    locked = true
    local vehicleName = player.Name.."Car"
    if game.Workspace:FindFirstChild(player.Name.."Car") then
        game.Workspace[player.Name.."Car"]:Destroy()
    end
    if (not workspace:FindFirstChild(vehicleName)) then
        local model = vehicles[index]:Clone()

        model.Name = vehicleName
        model.Parent = game.Workspace
        model:MakeJoints()
        model:MoveTo(HRP.Position + Vector3.new(10, 0, 0))
        --[[
        Uncomment the following statement to make the car spawn in front of you
        Use the above OR this following Line NOT Both! otherwise this next line will override the first one!
        Ref: move model ('Reference part position'  + (LookVector of the character  * 10 studs in front)+ Offset
]]
        -- |\/|\/|\/|\/|\/|\/|\/|\/| THIS! |\/|\/|\/|\/|\/|\/|\/|\/|
        -- model:MoveTo(HRP.Position + (HRP.CFrame.LookVector * 10) + Vector3.new(0,3,0)
        bg.Visible = false
    end 
    locked = false
end)

bg.B.MouseButton1Click:connect(function()
    index = index - 1
    if (index < 1) then index = #vehicles end
    SetInformation(index)
end)

bg.N.MouseButton1Click:connect(function()
    index = index + 1
    if (index > #vehicles) then index = 1 end
    SetInformation(index)
end)

SetInformation(index)

This should work since Roblox uses that Hidden part Named HumanoidRootPart for controling/moving characters.

If you have any problems, let me know!.

I've also added an optional bit of code that spawns the car in front of the character by 10 studs!

Hope this helps! :)

Ad

Answer this question