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

How do I make a model MoveTo() AROUND the player?

I am confused. I am wondering how do I make this model MoveTo() around the player. The road is 73 studs wide. I tried to make it move to the HumanoidRootPart, but it just moves the car to the center.

local random = Random.new()
local Cars = {5083437873, 5083676767}

while wait(1) do
    wait(0.1) --Frick
    whichNum = random:NextInteger(1, #Cars)
    print(("Picked: " .. whichNum))

    local root = game:GetService("InsertService"):LoadAsset(Cars[whichNum])

    local instances = root:GetChildren()
    if #instances == 0 then
        root:Destroy()
        return
    end

    root = root:GetChildren()
    if #root == 1 then
        root = root[1]
    else return end
    print(root)
    root.Body.Drive.Disabled = false
    root.Parent = workspace
	game.Players.PlayerAdded:connect(function(player)
		root:MoveTo(player.HumanoidRootPart.Position+Vector3.new(math.random(1,73),0,0))
	end)
end

1 answer

this is incredibly inefficient, you don't need the while loop.. The issue with your code seems to be on line 25.. there's 2 components to a roblox player, the player object - which contains all data about the user such as if they have premium, their account age, etc, and second the character - which is the physical character that can interact with other players and is in 3d space(workspace).. but you did is player.HumanoidRootPart, remember, the HumanoidRootPart is part of the character, therefore, you need to get access to the player's character to do what u intend to do..

for the inefficiency, all you need to do is listen for when a player joins the game, then load the car model, etc.. the while loop will endlessly insert cars and register thousands of functions to the player added event, and take a real toll on the performance of your game..

so do something like this:

local Players = game:GetService("Players");
local InsertService = game:GetService("InsertService");

local Cars = {5083437873, 5083676767}

Players.PlayerAdded:Connect(function(player)
	local character  = player.Character or player.CharacterAdded:Wait()--waits for the character if not already loaded

	local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
	
	local carId = Cars[math.random(1, #Cars)];
	local root = InsertService:LoadAsset(carId);

	  local instances = root:GetChildren()
	  if #instances == 0 then
	      root:Destroy()
	      return
	  end
	
	  root = root:GetChildren()
	  if #root == 1 then
	      root = root[1]
	  else return end
	  print(root)
	  root.Body.Drive.Disabled = false
	  root.Parent = workspace

	root.Position = HumanoidRootPart.Position
end)

9 comments

0

The cars are now spawning in the middle of the highway, not where the character is. ALSO I NEED THE LOOP SO IT CAN SPAWN MORE CARS.

0

Also the cars delete themselves after an amount of time.

0

just updated my answer

0

also, if root is a model, then use root:SetPrimaryPartCFrame to position the car

0

How do I use SetPrimaryPartCFrame?

0

Nvm about that. The cars are just exploding and you get flung into space. I need help.

0

Position the car a few studs away from the player's character..

0

But how?

0

character:GetPrimaryPartCFrame() - CFrame.new(offsetX, 0, offsetZ);

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