ModuleScript - Attempt to index nil with 'leaderstats'

Hello, I want to make function in the module to use it everywhere, but I can’t do it.

Whatever I do I always get the error: attempt to index nil with ‘leaderstats’. What am I doing wrong? Can someone help me?

Sorry if this question seems weird to you (I already tried to find the answer to my question, but I couldn’t.)
I tried to create variables but it doesn’t help.

ModuleScript -

local module = {}

function module.AddMoney(Player)
	Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 1
end

return module

ServerScript - 
local Module = require(game.ServerStorage.ModuleScript)

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		Module.AddMoney()
	end
end)

Attempt to index nil with “leaderstats” means that Player is nil (doesn’t exist), You are passing a nil arguement.

In the script when you trigger the function, print the player variable that you’re going to pass as an arguement and tell us if it prints nil

EDIT: Nevermind, i didn’t notice the serverscript, Galaxy below me has your answer.

Module.AddMoney()

You need to put a parameter in this function, which would be the player that touched the part in this case.

Module.AddMoney(game.Players:GetPlayerFromCharacter(hit.Parent))
2 Likes

I did in Server Script as you said but still get an error.

local Module = require(game.ServerStorage.ModuleScript)

script.Parent.Touched:Connect(function(Hit)
	Module.AddMoney(game.Players:GetPlayerFromCharacter(Hit.Parent))
end)

What’s the error that gets printed?

The same
Screenshot_1

I solved this problem by simply adding ModuleScript - if Player then

This errors again because you need to check if the part that touched is part of a player’s character.

script.Parent.Touched:Connect(function(Hit)
    if game.Players:GetPlayerFromCharacter(Hit.Parent) then
    	Module.AddMoney(game.Players:GetPlayerFromCharacter(Hit.Parent))
    end
end)

Edit: Nevermind, you already solved this.

1 Like