Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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.

0
exactly why it wont work TheluaBanana 946 — 4y
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 User#28017 0 — 4y
0
as I said. noammao 294 — 4y

2 answers

Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
4 years ago
Edited 4 years ago

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
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 Mr_Unlucky 1085 — 4y
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" User#28017 0 — 4y
Ad
Log in to vote
0
Answered by
noammao 294 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
the script is only supposed to change the cframe of the players that do have their characters loaded. User#28017 0 — 4y
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. noammao 294 — 4y
0
idk, maybe i did something in my world thats messing it up. ill look around and see whta the problem is User#28017 0 — 4y

Answer this question