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.
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
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.