RGB_CATT
RGB_CATT

Reputation: 37

Module script throwing error "attempt to call a nil value"

Here is my code

local module = {}

function module.function_name(target)
    _G.target = target
    local target = game.Players:WaitForChild(tostring(_G.target))
    script.ScreenGUI:Clone().Parent = target.PlayerGui
end

return module

But it returns

require(game.Workspace.code.ModuleScript).function_name("ihatescauming1234"):1: attempt to call a nil value

What i have tried: Changing

_G.target = target 

to

game.players.localplayer

i thought it would work before i realized i am running this on the server side

Upvotes: 0

Views: 1151

Answers (1)

proScripterBoi
proScripterBoi

Reputation: 36

so there's only ONE problem lol! This is why OOP may be confusing to someone

your code:

local module = {}

function module.function_name(target)
    _G.target = target
    local target = game.Players:WaitForChild(tostring(_G.target))
    script.ScreenGUI:Clone().Parent = target.PlayerGui
end

return module

the fix:

local module = {}
module.__index = module

function module.function_name(target)
    _G.target = target
    local target = game.Players:WaitForChild(tostring(_G.target))
    script.ScreenGUI:Clone().Parent = target.PlayerGui
end

return module

Upvotes: 2

Related Questions