Hello, I'm not an experienced scripter but I know a little about scripting and I'm working on my game here and I made a tool that whenever you activate it, it will teleport a horse model to your player, I'm having issues doing so and the script seems to not be working, Here is the script:
1 | game.Players.PlayerAdded:Connect( function (player) |
2 | local Tool = script.Parent |
3 | local Horse = game.ServerStorage.Horse:Clone() |
4 | local char = player.Character |
5 | local characterCFrame = player.Character.HumanoidRootPart.CFrame |
6 | Tool.Activated:Connect( function () |
7 | Horse:SetPrimaryPartCFrame(characterCFrame) |
8 | end ) |
9 | end ) |
The script is inside of the tool, which is inside of StarterPack. Any help would be very much appreciated, Thanks!
Use LocalScript in tools. LocalScript can't access ServerStorage. SetPrimaryPartCFrame is deprecated (use PivotTo). Use game.Players.LocalPlayer instead of PlayerAdded. You should learn more about Roblox scripting before making your game...
1 | local Tool = script.Parent |
2 | local Horse = game.ReplicatedStorage.Horse:Clone() |
3 | local char = game.Players.LocalPlayer.Character |
4 | local characterCFrame = player.Character.HumanoidRootPart.CFrame |
5 | Tool.Activated:Connect( function () |
6 | Horse:PivotTo(characterCFrame) |
7 | end ) |
I don't recommend using Players.PlayerAdded
because when you use to the tool, the horse might be teleported to all players instead of just you. Instead, get the Parent of the tool and check if it's a player. When the game starts, everything inside StarterPack will be transferred to the backpack of the players. If the player equip the tool, the tool leaves Player.Backpack
and transferred to Player.Character
. To check if the tool has been equipped, you use Tool.Equipped
. This event returns a mouse, but it's optional.
To get the player's character, we simply get the Tool's Parent only if it's equipped. To detect if it's a real player, we simply do Players:GetPlayerFromCharacter()
and check if it's nil (nothing/non-existent/null/void) or not.
And yes, the first person who answered this is correct: use PVInstance:PivotTo()
and PVInstance:GetPivot()
instead.
Make sure the script is a normal script.
So the final solution would be:
01 | local Players = game:GetService( "Players" ) |
02 | local ServerStorage = game:GetService( "ServerStorage" ) |
03 |
04 | local Horse = ServerStorage.Horse |
05 | local Tool = script.Parent |
06 | Tool.Equipped:Connect( function (Mouse) |
07 | local Character = Tool.Parent |
08 | local Player = Players:GetPlayerFromCharacter(Character) |
09 |
10 | if Player ~ = nil then -- checks if it's a real player |
11 | Tool.Activated:Connect( function () |
12 | local characterCFrame = Character:GetPivot() |
13 |
14 | local horseClone = Horse:Clone() |
15 | horseClone:PivotTo(characterCFrame) |
16 | end ) |
17 | end |
18 | end ) |
weld a part to your entire model then teleport that part to wherever u want
1 | local Tool = script.Parent |
2 | local Horse = game.ReplicatedStorage.Horse:Clone() |
3 | local char = game.Players.LocalPlayer.Character |
4 | local characterCFrame = player.Character.HumanoidRootPart.CFrame |
5 | Tool.Activated:Connect( function () |
6 | Horse.Parent = workspace |
7 | Horse.TPPart.CFrame = characterCFrame -- replace TPPart with your part thats welided to your model |
8 | end ) |
This could be done using RemoteEvent. you will need to create a RemoteEvent with any name then a script with this script:
1 | local Tool = script.Parent |
2 | local RemoteFires = -- path of the remote event, Ex: Tool.RemoteEvent, Tool.Folder.RemoteEvent, Tool.Model.RemoteEvent, etc. |
3 |
4 | Tool.Activated:Connect( function () |
5 | local Player = game:GetService( "Players" ) |
6 | local HumanoidPartCF = Player.LocalPlayer.Character.HumanoidRootPart.CFrame |
7 | RemoteFires:FireServer(HumanoidPartCF) |
8 | end ) |
then when it fires the remote event, the serverscript will detect it and gets the information about the event fired
01 | local RemoteFires = -- path of the remote event, Ex: Tool.RemoteEvent, Tool.Folder.RemoteEvent, Tool.Model.RemoteEvent, etc. |
02 |
03 | RemoteFires.OnServerEvent:Connect( function (player, CFrame) |
04 | local CFramePlayer = CFrame |
05 | local CloneHorse = game.ReplicatedStorage.Horse:Clone() |
06 | local parentclonedpart = CloneHorse |
07 | parentclonedpart.Parent = game.Workspace |
08 |
09 | local centerhorsepart = -- inside the horse, it must have a part where the whole horse model is welded to the part using WeldConstraint |
10 | centerhorsepart.CFrame = CFramePlayer |
11 |
12 | end ) |
This could maybe work, because i havent tested yet but hope this helps!