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

GetPlayerFromCharacter error? [ANSWERED]

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):-

function teleportcontestants() -- Function
	for _,TeleportContestants in pairs(game.Players:GetPlayerFromCharacter(character)) do -- Gets the characters model
		character.CFrame = workspace.MyPart -- Teleports the character to the part.
	end
end

Here is the script (With no annotation):-

function teleportcontestants() 
	for _,TeleportContestants in pairs(game.Players:GetPlayerFromCharacter(character)) do 
		character.CFrame = workspace.MyPart 
	end
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.

1 answer

✓ Accepted answer

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

function teleportcontestants() -- Function
    for _,TeleportContestants in pairs(game:GetService("Players"):GetChildren()) do -- Gets the children of players
	if TeleportContestants.ClassName == "Player" then --Check if the object is a player.
		--[[Also, I do ClassName instead of IsA for players because players are Instances, same with
booleans and other things like that.]]
	local character = TeleportContestants.Character
        character:SetPrimaryPartCFrame(workspace.MyPart.CFrame * CFrame.Angles(0,math.rad(45),0)) 
-- 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().
    end
end

Hope it helps!

1 comment

0

Works great! Thanks!

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