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

ServerScriptService.Spawn_CFrameScript:5: attempt to index field 'Character' (a nil value)?

Asked by 4 years ago

Script is supposed to move a player to a safe area when they join the game/server, however trying to get mulitple players to join gives me this error:

"ServerScriptService.Spawn_CFrameScript:5: attempt to index field 'Character' (a nil value)"

Here's the script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    repeat wait() until
    player.Character:WaitForChild("HumanoidRootPart").CFrame ==
     workspace._SpawnHub.SpawnHub.CFrame
end)

Even this configuration gives the same error too.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    wait()
    player.Character:WaitForChild("HumanoidRootPart").CFrame =
     workspace._SpawnHub.SpawnHub.CFrame
end)

It's probably a real easy fix but I just can't seem to get my eye on what is wrong, so I'm turning this towards the professionals. What is wrong with my script and what needs to be changed, so it works correctly?

5 answers

Log in to vote
0
Answered by 4 years ago

It's easier to just do everything as one.

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    if character then
        local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if HumanoidRootPart then
            local safeSpot = game:GetService("Workspace").Part
            wait()
            HumanoidRootPart.CFrame = safeSpot.CFrame + Vector3.new(0,5,0)
        end
    end
end)

This is in a ServerScript in ServerScriptService

0
This should work, if it doesn't let me know so I can fix it for you. killerbrenden 1537 — 4y
0
It works perfectly! Thank you for taking time out of your day to help me. ThaDogeTho 11 — 4y
0
No problem! killerbrenden 1537 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

The problem is simple, when a player joins their character isn't loaded yet, but this script expects it to be. Try this script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:wait() -- Waits till the character is added
    player.Character:WaitForChild("HumanoidRootPart").CFrame =
     workspace._SpawnHub.SpawnHub.CFrame
end)
0
Tried it, it unfortunately doesn't work, however the only difference is I don't get an error in the output. ThaDogeTho 11 — 4y
1
wait what? it doesn't give you any error- Oh wait, I might see what's wrong, you teleport the HumanoidRootPart. Try the UpperTorso if you use R15 (for R6 players it's called Torso) BeAHacker666 75 — 4y
Log in to vote
0
Answered by 4 years ago

I would Suggest Trying a "Local Script" instead of a "Script then adding a Variable "local player = Players.LocalPlayer"

Log in to vote
0
Answered by
8_zn 21
4 years ago
Edited 4 years ago

I'm not really good at explaining so, the script should be located in ServerScriptService and should be a server script.

--//Services\\--
local Players = game:GetService("Players")

--//Script\\--
Players.PlayerAdded:Connect(function(Player)
    local Character = Player.Character
    local humanoidRootPart = Character:WaitForChild("HumanoidRootPart")
    local spawnHub = workspace._SpawnHub.SpawnHub
    humanoidRootPart.CFrame = CFrame.new(Vector3.new(spawnHub.Position.X, spawnHub.Position.Y, spawnHub.Position.Z))
end)

P.S. if the code above works, please mark it as an answer.

Log in to vote
0
Answered by 4 years ago

Try this script

local Players = game:GetService("Players")

 Players.PlayerAdded:Connect(function(plr)
    repeat wait() until workspace[plr.Name]
    local char = workspace[plr.Name]
    local HRP = char:WaitForChild("HumanoidRootPart")

    HRP.CFrame = CFrame.new(workspace._SpawnHub.SpawnHub.CFrame)
end)




Answer this question