Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I make it where a npc dies it gives everyone cash?

So I am making something where a npc dies it gives everyone cash, I am having a problem where it won't even give out the cash, please help!!

Code:

local player = game.Players:GetPlayers()

script.Parent.Humanoid.Died:Connect(function()
	for i,v in pairs(player) do
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 500000
	end
end)

Error: 02:36:36.236 - Workspace.Boss1.Script:5: attempt to index nil with 'Coins'

3 answers

I’m pretty sure that you can only use player.leaderstats when you are dealing with 1 player.

The problem is not in the script but in leaderstats, it's nil, check if they are named 'leaderstats' or if their parent is where you said. Try checking if the leaderstats are not nil.

You are using the player argument as a table, thus the table trying to find the leaderstats value inside the player. Try this script.



script.Parent.Humanoid.Died:Connect(function() -- wait for humanoid to die
local players = game.Players:GetPlayers() -- get the players table
    for i,player in pairs(players) do -- loop through the players table
        player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 500000 -- add money
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.