Hi. I am making an FPS on ROBLOX. I was trying to make a script where when somebody clicks the "Deploy" button... it teleported the players character from the pre - area to an actual spawn in the game. I didn't want to kill the person to send them there s I used the CFrame technique. My problem is I keep getting an error message. Here's my script:
local button = script.Parent --Deploy Button local plrs = game:GetService("Players") --Gets Players Service local player = game.Players.LocalPlayer --Defines The Local Player local numofspwns = 10 --The Number Of Spawns local target = game.Workspace.MapSpawns[math.random(1, 10)] --Defines what tp brick will be chosen local function Teleport() --Function For TP if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, 5, 0) end end local function Spawn() --Defines What A Spawn Is (I didn't include any other code to prevent copiers.) button.Parent.Parent.Enabled = nil Teleport() end local function onButtonClick() --When clicked... Spawn() --Spawn. end button.MouseButton1Click:connect(onButtonClick) --Defines onButtonClick() func.
The Error message I keep getting is: 22:28:38.921 - Players.ScrappyFriend77.PlayerGui.ScreenGui.MainFrame.Deploy.LocalScript:9: bad argument #1 to '?' (Vector3 expected, got Object)
Full Output: 22:28:38.921 - Players.ScrappyFriend77.PlayerGui.ScreenGui.MainFrame.Deploy.LocalScript:9: bad argument #1 to '?' (Vector3 expected, got Object) 22:28:38.922 - Stack Begin 22:28:38.924 - Script 'Players.ScrappyFriend77.PlayerGui.ScreenGui.MainFrame.Deploy.LocalScript', Line 9 - upvalue Teleport 22:28:38.925 - Script 'Players.ScrappyFriend77.PlayerGui.ScreenGui.MainFrame.Deploy.LocalScript', Line 15 - upvalue Spawn 22:28:38.927 - Script 'Players.ScrappyFriend77.PlayerGui.ScreenGui.MainFrame.Deploy.LocalScript', Line 19 22:28:38.929 - Stack End
Thanks for taking your time to see this. Whoever answers this will get an extra perk in the game. Thanks again. (Perk will probably be access to all guns. I'm a nice guy. lol)
First of all, stacks happening is not a error. What your error message means, is that you're trying to get a position from a part without accessing the part's property, position. This is basically what you did.
workspace.Part.CFrame = workspace.TeleportBlock
instead of doing
workspace.Part.CFrame = workspace.TeleportBlock.Position
So you should do
player.Character.Torso.CFrame = target.Position + CFrame.new(0, 5, 0)
on line 9.
There was only one thing you needed to do You needed to change the part where it says player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, 5, 0) to player.Torso.CFrame = target + CFrame.new(0, 5, 0) Its torso instead of HumanoidRootPart because I have done a lot of teleporting systems and I always use Torso as the teleporting object since it gets the whole character Oh btw I recommend WaitForChild() instead of FindFirstChild() And also , connect is depricated, I recommend Connect
local button = script.Parent --Deploy Button local plrs = game:GetService("Players") --Gets Players Service local player = game.Players.LocalPlayer --Defines The Local Player local numofspwns = 10 --The Number Of Spawns local target = game.Workspace.MapSpawns[math.random(1, 10)] --Defines what tp brick will be chosen local function Teleport() --Function For TP if player.Character and player.Character:WaitForChild("HumanoidRootPart") then player.Character.Torso.CFrame = target + CFrame.new(0, 5, 0) end end local function Spawn() --Defines What A Spawn Is (I didn't include any other code to prevent copiers.) button.Parent.Parent.Enabled = nil Teleport() end local function onButtonClick() --When clicked... Spawn() --Spawn. end button.MouseButton1Click:Connect(onButtonClick) --Defines onButtonClick() func. --rest of code here-- end)
I would be very happy to play the FPS you are developing! Click here for you to see what CFrame is and how it works and other syntaxes and how to use arithmetic with CFrame to make your game proffesional Please reply or submit this answer for me to make sure that this fixed your script, Hopefully this answered you question! PS. Whenever you use CFrame the code on the right must have the CFrame syntax For Example : script.Parent.CFrame = CFrame.new(pos)