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?
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
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)
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)