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

How do you get the character from player?

Asked by
sad_eyez 162
7 years ago

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

Log in to vote
1
Answered by 7 years ago

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
Ad

Answer this question