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

Player's Character is not being defined, please help?

While trying to make a round-based game, I came across a problem in the following script:

local plrs = game.Players:GetChildren()
	for i = 1, #plrs do
		local char = plrs[i]:FindFirstChild("Character") 
		if char then 
			char.HumanoidRootPart.CFrame = workspace.gameSpawn.CFrame
			print("true fam")
		end
	end

When playing the game, my player's Character is not having its CFrame changed. "true fam" is not printed even when my player's Character is fully loaded. Please help if you know how this problem is occurring.

No, this code is not inside a LocalScript. It's in a server script in ServerScriptService.

3 comments

0

exactly why it wont work

0

i tested to see if not char, and the code in that processed. basically, the player's character is not being referred to with the local char variable for whatever reason

0

as I said.

2 answers

✓ Accepted answer

The reason why it isn't working is because you're using :FindFirstChild() when the Player's character is not a child of the Player Instance.

local plrs = game:GetService("Players")
for i,v in pairs(plrs:GetPlayers()) do
	local char = v.Character or v.CharacterAdded:Wait()
	local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
	if char and HumanoidRootPart then
		HumanoidRootPart.CFrame = workspace.gameSpawn.CFrame
		print("true fam")
	end
end

2 comments

0

I made like 1234234234 more edits to this answer but the thing is there were some stuff I needed to change and then afterwards I realize another thing I have to change without even realizing it

0

there were multiple errors in this code, even after fixing them the game then told me that "HumanoidRootPart is not a valid member of Player"

I'm not sure since I usually don't use those hashtags and [i] & stuff. But this will work:

local Players = game:GetService("Players")
wait(3)
for i,v in pairs(Players:GetChildren()) do
	local char = v.Character
	
        if char then 
            char.HumanoidRootPart.CFrame = workspace.asd.CFrame
            print("true fam")
	end
end

The reason I have the 3 seconds delay (and I think it might be the problem in your case as well) is that the loops start running as soon as it can. And because scripts load faster than the characters & because that in this instance you don't use a loop or an event, the code will fire once, and that is before the models have loaded into the game. Therefore char is not equal to true.

3 comments

0

the script is only supposed to change the cframe of the players that do have their characters loaded.

0

Then i don't get what the problem is. Both my & MrUnlucky's codes work perfectly fine as long as a character has loaded.

0

idk, maybe i did something in my world thats messing it up. ill look around and see whta the problem is

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