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

Using MoveTo:() Won't move the model to where I tell it to?

I'm trying to get a horse model to move the player's position, here is the code that I'm using:

script.Spawn.OnServerEvent:Connect(function(Player)
	if workspace:FindFirstChild("".. Player.Name .. "'s Horse") == nil and Player.Stats.HasHorse.Value == 1 then
		local Horse = game.ReplicatedStorage.Horses.BasicHorse:Clone()
		local Part = Player.Character.HumanoidRootPart.Position
		Horse.Name = "".. Player.Name .. "'s Horse"
		Horse:MoveTo(Part)
		Horse.Parent = workspace	
	elseif workspace:FindFirstChild("".. Player.Name .. "'s Horse") ~= nil and Player.Stats.HasHorse.Value == 1 then
		workspace:FindFirstChild("".. Player.Name .. "'s Horse"):Destroy()
	end
end)

Instead of moving it to the Player's RootPart, it just moves it to some random place in the workspace. Example

Thanks for reading, and I'd appreciate any help that you can offer

1 comment

0

Edited'

1 answer

✓ Accepted answer

###MoveTo has weird behaviour when it comes to placing it on parts. Set a PrimaryPart to the horse and use SetPrimaryPartCFrame. You should not use MoveTo for cases like yours.

###I also fixed a few things that weren't necessary, such as on your FindFirstChild's. You're welcome.

script.Spawn.OnServerEvent:Connect(function(Player)
    if not game.Workspace:FindFirstChild(Player.Name .. "'s Horse") and Player.Stats.HasHorse.Value == 1 then
        local Horse = game:GetService("ReplicatedStorage").Horses.BasicHorse:Clone()
        Horse.Name = Player.Name .. "'s Horse"
        Horse.Parent = game.Workspace
        Horse:MakeJoints() -- to make sure the horse doesn't fall apart 
        Horse:SetPrimaryPartCFrame(Player.Character.HumanoidRootPart.CFrame)
    elseif game.Workspace:FindFirstChild(Player.Name .. "'s Horse") and Player.Stats.HasHorse.Value == 1 then
        game.Workspace[Player.Name.."'s Horse"]:Destroy()
    end
end)

###You don't need the extra concatenations, neither the == nils.

4 comments

0

Thanks for your help, but that code gives me an error: "SetPrimaryPart is not a valid member of model"

0

Oh sorry, I forgot the CFrame lol. It's SetPrimaryPartCFrame

0

Line 7 got cut off. It's Player.Character.HumanoidRootPart.CFrame

0

Now I'm getting :"Model:SetPrimaryCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this."

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