[SOLVED] LocalPlayer() (a nil value)

Hello i have created a simple hook wich shoud check if a Player has a specifig weapon but when i save the file my Console is getting spammed with a error [ attempt to call global “LocalPlayer” (a nil value)

hook.Add("Think", "Ris_Hidden_Blade_Think", function()
if ply:IsValid() then
    local weapon = ply:GetActiveWeapon()

    if weapon:IsValid() and klassen[weapon:GetClass()] and input.IsKeyDown(KEY_E) and modetime < CurTime() and killmode == false and stunmode == true then
        modetime = CurTime() + 1
        print("Killmode Aktiviert")
        killmode = true
        stunmode = false
    elseif input.IsKeyDown(KEY_R) and killmode == true and stunmode == false then
    	modetime = CurTime() + 1
        print("Stunmode Aktiviert")
        killmode = false
        stunmode = true
    end
elseif ply == nil then
    return
end
end) 

Here is the code hopefully someone can help me

Is the text on the console written in blue or orange? If it’s blue then this means that the code is being called on the server.

Also please do not make global variables if you’re only going to be using them inside of this hook.

1 Like

The error will be coming from somewhere else where as your code doesn’t contain LocalPlayer anywhere.

2 Likes

Good catch, didn’t even notice. My guess is that he has ply set to a global variable somewhere else with it returning LocalPlayer().

EDIT: Having a good look at this, everything used is a global variable (Or hasn’t been set)
Can we get the full code?

1 Like

The Error is Blue

local klassen = {"weapon_rpg"}
local modetime = 0
local killmode = false
local stunmode = true
hook.Add("Think", "Ris_Hidden_Blade_Think", function()
    local ply = LocalPlayer()
    if !ply:IsValid() then
        return -- return, when LocalPlayer is not valid
    end

    local weapon = ply:GetActiveWeapon()
    if weapon:IsValid() and klassen[weapon:GetClass()] and input.IsKeyDown(KEY_E) and modetime < CurTime() and killmode == false and stunmode == true then
        modetime = CurTime() + 1
        print("Killmode Aktiviert")
        killmode = true
        stunmode = false
    elseif input.IsKeyDown(KEY_R) and killmode == true and stunmode == false then
        modetime = CurTime() + 1
        print("Stunmode Aktiviert")
        killmode = false
        stunmode = true
    end
end)

maybe something like that?

1 Like

If the error is blue, then the error is server-side, your code is running on the wrong realm - you can use LocalPlayer only on the client realm.

Oh yes