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

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

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

✓ Accepted answer

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

3 comments

0

This should work, if it doesn't let me know so I can fix it for you.

0

It works perfectly! Thank you for taking time out of your day to help me.

0

No problem!

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)

2 comments

0

Tried it, it unfortunately doesn't work, however the only difference is I don't get an error in the output.

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)

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

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.

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)
	
	

	

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