How to get the local player in a module script

Hello Devs,
I have this module script here but I cant get the local player.
I would like some help to get this done.

local module = {}
function module.PlrsBanned(Plr)
    local Button = Plr.PlayerGui:WaitForChild("AdministratorSaves").PlrBanVal
    Button = Button + 1
end

return module

any help is appreciated!
and here is the error it is giving me

StarterGui.AdministratorPanel.Scripts.Modules.Home:3: attempt to index nil with 'PlayerGui'  -  Client - Home:3
  Stack Begin  -  Studio
  Script 'StarterGui.AdministratorPanel.Scripts.Modules.Home', Line 3 - function PlrsBanned  -  Studio - Home:3
  Script 'Players.Paysallday44.PlayerGui.ScreenGui.TextButton.LocalScript', Line 4
1 Like

could you say how you use that module

1 Like

well there is a gui evey time the ban button is pressed the PlrBanVal value’s goes by 1 so plus 1

no no i mean script line where you used that module

1 Like
1 local module = {}
2 function module.PlrsBanned(Plr)
3   local Button = Plr.PlayerGui:WaitForChild("AdministratorSaves").PlrBanVal
4    Button = Button + 1
5  end
6
7  return module

i dont know what exactly your talking about but i think this will help?

no no i mean how you are using it like in other script not in module

1 Like

like this?
local module = require(module)
module.PlrsBanned(game.Players.plrname)

1 Like
local Home = require(game.StarterGui.AdministratorPanel.Scripts.Modules.Home)

script.Parent.MouseButton1Click:Connect(function()
    Home.PlrsBanned()
end)
```

it should be like home.PlrsBanned(player here)

2 Likes

as the above person said,
Home.PlrsBanned()
should be

Home.PlrsBanned(game.Players.LocalPlayer)

Writing it out here so you know what is being referred to

1 Like

thanks for that but its still giving me the error for the module script
what do i change there?

wait maybe try this:

local Home = require(game.Players.LocalPlayer.PlayerGui.AdministratorPanel.Scripts.Modules.Home)

script.Parent.MouseButton1Click:Connect(function()
    Home.PlrsBanned(game.Players.LocalPlayer)
end)
1 Like

Im talking about this

local module = {}
function module.PlrsBanned(Plr)
    local Button = Plr.PlayerGui:WaitForChild("AdministratorSaves").PlrBanVal
    Button = Button + 1
end

return module

what do i change here

Right.

Try this:

ModuleScript

return function(Player)
	local Button = Player["PlayerGui"]:WaitForChild("AdministratorSaves")["PlrBanVal"]
	Button = Button + 1
end

LocalScript

local StarterGUI	= game:GetService("StarterGui")
local Players		= game:GetService("Players")
local LocalPlayer	= Players["LocalPlayer"]

local Home			= require(StarterGUI["AdministratorPanel"]["Scripts"]["Modules"]["Home"]) -- is the module script in this instance? if so then do ["ModuleScript"]

local Button		= script["Parent"]

Button["MouseButton1Click"]:Connect(function()
	Home(LocalPlayer)
end)

Edit: Wait, are you even requiring the ModuleScript itself?
Can you post a screenshot of your workspace where the ModuleScript is and where the LocalScript is?

Edit 2: NVM, that was me being a dumb dumb, I forgot you can rename Instances.

1 Like

Ok the script works but this error occurs

Infinite yield possible on 'Players.Paysallday44.PlayerGui:WaitForChild("AdministratorSaves")'  -  Studio
  Stack Begin  -  Studio
  Script 'StarterGui.AdministratorPanel.Scripts.Modules.Home', Line 3 - function PlrsBanned  -  Studio - Home:3
 Script 'Players.Paysallday44.PlayerGui.ScreenGui.TextButton.LocalScript', Line 4  -  Studio - LocalScript:4
  Stack End

You can get local player from module.

local module = {}
function module.PlrsBanned()
    if game.Players.LocalPlayer then -- If module got required from local script then
      local Plr = game.Players.LocalPlayer
      local Button = Plr.PlayerGui:WaitForChild("AdministratorSaves").PlrBanVal
      Button = Button + 1
    else
          -- module got required from server script.
    end
end

return module
1 Like

Thats not an error, thats a warning I suppose.

1 Like

Ah,
So the ModuleScript was looking for “AdministratorSaves” Instance in the PlayerGUI but it can’t find it, hence the reason for yielding infinitely.

Can you post a screenshot of your StarterGUI?

1 Like

the AdministratorSaves is a instance.new function
image

1 Like

So…
I did a little bit of debugging.

You can skip this part but this is what i tried:
ModuleScript

return function(Player)
	local Button = Player["PlayerGui"]:WaitForChild("AdministratorSaves")
	print(Button["Name"])
end

LocalScript

local StarterGUI	= game:GetService("StarterGui")
local Players		= game:GetService("Players")
local LocalPlayer	= Players["LocalPlayer"]

local Home			= require(StarterGUI["AdministratorPanel"]["Scripts"]["Modules"]["Home"]) -- is the module script in this instance? if so then do ["ModuleScript"]

local Button		= script["Parent"]

local AdministratorSaves = Instance.new("ScreenGui")
AdministratorSaves["Name"] = "AdministratorSaves"
AdministratorSaves["Parent"] = LocalPlayer["PlayerGui"]

Button["MouseButton1Click"]:Connect(function()
	Home(LocalPlayer)
end)

Try to tweak your Instance.new to this:

local AdministratorSaves = Instance.new("ScreenGui") -- change it if its not a ScreenGui
AdministratorSaves["Name"] = "AdministratorSaves"
AdministratorSaves["Parent"] = LocalPlayer["PlayerGui"]
2 Likes