Ad
Log in to vote
1

Why does my script not know what HumanoidRootPart is? The script tells it what it is

Asked by
MattVSNNL 614 Moderation Voter
1 year ago

I'm making a game for fun and I'm coding the MainFramework but with teleporting it says Attempted to index with a nil value:WaitForChild(), Can anyone help me?

Here's the code:

local EventsModule = require(script:WaitForChild("EventsModule"))
local MethodNames = {}

for MethodName, Method in pairs(EventsModule) do
    if type(Method) == "function" then
        table.insert(MethodNames, MethodName)
    end
end

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local Values = ReplicatedStorage:WaitForChild("Values")
local Status = Values:WaitForChild("Status")

local timeBetweenEvents = 5
local AllPlates = ReplicatedStorage:WaitForChild("Plates"):GetChildren()

local minimumPlayers = 0

while true do
    Status.Value = "Waiting for enough players!"

    repeat wait(1) until game.Players.NumPlayers >= 1

    Status.Value = "Intermission"

    local Plates = ReplicatedStorage.Plates:Clone()
    Plates.Parent = workspace

    local Players = game.Players:GetPlayers()
    local PlayersAlive = {}
    local PlatesAlive = {}

    for i, player in pairs(game.Players:GetPlayers()) do
        local plate = Plates:FindFirstChild("Plates"..i)
        local character = player.Character

        local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
        if HumanoidRootPart and #Players >= minimumPlayers then
            HumanoidRootPart.CFrame = plate.CFrame * CFrame.new(0,3,0)
            player.serverData.playerPlate.Value = i

            local plateOwner = Instance.new("StringValue")
            plateOwner.Name = "plateOwner"
            plateOwner.Value = player.Name
            plateOwner.Parent = plate

            table.insert(PlayersAlive, player)
            table.insert(PlatesAlive, plate)
            for i, v in pairs(PlayersAlive) do print(i,v) end

            character.Humanoid.Died:Connect(function()
                print(player.Name.." has died")

                table.remove(PlayersAlive, table.find(PlayersAlive, player))
                table.remove(PlatesAlive, table.find(PlatesAlive, plate))
            end)
        end
    end

    for i, plate in pairs(Plates:GetChildren()) do
        local plateOwner = plate:FindFirstChild("plateOwner")
        if plateOwner.Value == nil then
            plate:Destroy()
        end
    end

    while #PlayersAlive > 0 do
        wait(timeBetweenEvents)

        local RandomMethodName = MethodNames[math.random(1, #MethodNames)]
        local Method = EventsModule(RandomMethodName)
        Method()
    end

    Plates:Destroy()
end
0
Try using player.CharacterAdded:Wait() on line 37? Omq_ItzJasmin 666 — 1y
0
ok MattVSNNL 614 — 1y

1 answer

Log in to vote
0
Answered by
TGazza 1301 Moderation Voter
1 year ago

Sounds like the script is running before each player has fully loaded. I've written an example on what happens when a player joins and when their character loads. The script(s) add the new player to a global table and waits till their character loads before returning the character in a Global table.

Caller Script:

local PlayersGroup = game:GetService("Players")
local SSS = game:GetService("ServerScriptService")
local PlayersMod = require(SSS:WaitForChild("Players Module"))

PlayersGroup.PlayerAdded:connect(function(plr) PlayersMod:addPlayer(plr) end)

while true do
    local Players = PlayersMod:getPlayers()
    for k,plr in pairs(Players) do
        print(k, ".Character = ",plr.Character)
    end 
    wait()
end

And the module script:

local plrs = {
    Players = {}
}

function plrs:addPlayer(plr)
    self.Players[plr] = {
        Character = nil
    }
    while plr.Character == nil do wait() end -- wait for the character to load before continuing!
    plrs.Players[plr].Character = plr.Character -- Set this players table entry to their character model
end

function plrs:removePlayer(plr)
    if(self.Players[plr] == nil) then
        warn(tostring(plr).." is not part of the players table!") 
    else
        plrs.Players[plr] = nil
    end
end
function plrs:getPlayers()
    return plrs.Players
end

function plrs:getPlayer(plr)
    if(self.Players[plr] == nil) then
        warn(tostring(plr).." is not part of the players table!") 
    else
        return plrs.Players[plr]
    end
end

return plrs

The module script is kinda a hacked together thing, but it works as an example.

The only two function i've tested is getPlayers() and addPlayer() the other two should work, but they are untested and might need a little work.

hope this helps!

Ad

Answer this question