Can anyone see an error in this if statement?

What is the error? So basically when the player touches the block and if they have the value = or over 3 they get the cash

The error outputed in the output log is this " 15:11:55.994 - Workspace.MoneyPunch.Script:6: attempt to index nil with ‘JewerlyPunch’"

local bin = script.Parent
local function onTouched(bin)
	
	local plr = game.Players:GetPlayerFromCharacter(bin.Parent)
	
	if plr.JewerlyPunch.Punch.Value >= 3 then
		plr.Stats.Credits.Value = plr.Stats.Credits.Value + 1000
	end				
end
bin.Touched:connect(onTouched)

The leaderboard nothing is wrong with this, this is to help

game.Players.PlayerAdded:connect(function(p)
    local stats = Instance.new("Folder")
    stats.Name = "JewerlyPunch"
    stats.Parent = p

    local punch = Instance.new("IntValue")
    punch.Name = "Punch"
    punch.Parent = stats

    local here = Instance.new("BoolValue")
    here.Name = "Here"
    here.Parent = stats
    here.Value = false

end)

1 Like

You are always assuming a player will always be the one touching, just check that there is a player first:

if plr and plr.JewerlyPunch.Punch.Value >= 3 then
1 Like

It’s wierd there is now no error but the credits don’t change?

1 Like

That is referred to as a logic error. Diagnose it by checking your math and variables.
Before the if statement, print plr and plr.JewelryPunch.Punch.Value, you will see where the issue lies.

3 Likes

I may be mistaken, but I believe its that when you created your values you parent ‘Punch’ to stats, but in your IF statement you show it parented to the JewelryPunch folder, so when you run your statement you are looking for a value in the JewelryPunch folder that is not there; i.e. "nil’.

‘stats’ is the JewelryPunch folder.