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

How do you get the character from player?

I need help getting the character from player, it keeps saying char is a nil value:

local plr = game.Players.LocalPlayer
local char = plr.Character

if not character or not character.Parent then
    character = plr.CharacterAdded:wait()
end

local chaos = realms:WaitForChild("Chaos")

chaos.MouseButton1Down:connect(function()
	if plr.leaderstats.Level.Value >= 500 then
		char.Torso.CFrame = CFrame.new(777.8, 414.156, -667.8)
	end
end)

1 answer

✓ Accepted answer

You're checking for character in your if statement, not char. Also, that if statement is unnecessary, you can just use the or operator after getting the character if it returns nil upon retrieving it the first time.

local player = game:GetService("Players").LocalPlayer
-- Returns the character, or waits for it to load, then returns it.
local char = player.Character or player.CharacterAdded:Wait()

print(char) -- Prints the character every time

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