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

Why does it not give the specific player 100 coins?

local grass = game.Workspace.GrassBlocks:GetChildren()

game.Players.PlayerAdded:Connect(function(plr)
for i,v in pairs(grass) do
	local name = plr.Name
    v.ClickDetector.MouseClick:Connect(function()
    name.leaderstats.Coins.Value = name.leaderstats.Coins.Value + 100
    v.Transparency = 1
    v.CanCollide = false
    wait(2)
    v.Transparency = 0.5
    wait(1)
    v.Transparency = 0
    v.CanCollide = true
end)
end
end)

That's a script in ServerScriptStorage. Error: 17:47:41.119 - ServerScriptService.Script:7: attempt to index field 'leaderstats' (a nil value) The leaderstats script

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Parent  = plr
	stats.Name = "leaderstats"
	
	local Coins = Instance.new("IntValue")
	Coins.Parent = stats
	Coins.Name = "Coins"
end)

2 answers

✓ Accepted answer

The event MouseClick has a player argument, you wouldn't need to have a PlayerAdded event.

Example:

local Grass = Model:GetChildren()

for _,v in pairs(Grass) do
	v.ClickDetector.MouseClick:Connect(function(Player)
		print(Player.Name, "clicked")
	end)
end
local grass = game.Workspace.GrassBlocks:GetChildren()

game.Players.PlayerAdded:Connect(function(plr)
	for i,v in pairs(grass) do
	    v.ClickDetector.MouseClick:Connect(function()
		    plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 100
		    v.Transparency = 1
		    v.CanCollide = false
		    wait(2)
		    v.Transparency = 0.5
		    wait(1)
		    v.Transparency = 0
		    v.CanCollide = true
		end)
	end
end)

You were trying to index a string I modified it so it indexes the player directly plr.Name.leaderstats is nil because plr.Name is a string plr.leaderstats would be the correct way to do it

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