Hello Scripting Helpers!
I just made a teleport script what uses GetPlayerFromCharacter. I'm not quite sure if I'm using it right. Here is the script (With annotation):-
1 | function teleportcontestants() -- Function |
2 | for _,TeleportContestants in pairs (game.Players:GetPlayerFromCharacter(character)) do -- Gets the characters model |
3 | character.CFrame = workspace.MyPart -- Teleports the character to the part. |
4 | end |
5 | end |
Here is the script (With no annotation):-
1 | function teleportcontestants() |
2 | for _,TeleportContestants in pairs (game.Players:GetPlayerFromCharacter(character)) do |
3 | character.CFrame = workspace.MyPart |
4 | end |
5 | end |
I was just looking at http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter to try and figure the GetPlayerFromCharacter.
It errors on line 2 and says bad argument #1 to 'pairs' (table expected, got nil).
It's the first time I'm using CFrame properly.
Any help?
Thanks! Nathan.
Because Character isn't a table. It's an object. To get a table you can do things like :GetChildren()
The next problem is you are not addressing who or what "character" is.
Since character is a model, you can't change it's CFrame. You may use a function called :SetPrimaryPartCFrame()
to change the CFrame. Remember, the character's primary part is the HEAD, position MyPart to where you want the player's head to be.
Lastly, on line 3 you need a CFrame/Vector3 value there. It's like asking "What color is 5?"
Finished script
01 | function teleportcontestants() -- Function |
02 | for _,TeleportContestants in pairs (game:GetService( "Players" ):GetChildren()) do -- Gets the children of players |
03 | if TeleportContestants.ClassName = = "Player" then --Check if the object is a player. |
04 | --[[Also, I do ClassName instead of IsA for players because players are Instances, same with |
05 | booleans and other things like that.]] |
06 | local character = TeleportContestants.Character |
07 | character:SetPrimaryPartCFrame(workspace.MyPart.CFrame * CFrame.Angles( 0 ,math.rad( 45 ), 0 )) |
08 | -- Teleports the character to the part. You can also change the player's Angle so that they face a certain way. Make sure you use math.rad(). |
09 | end |
10 | end |
Hope it helps!