New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Why does it not give the specific player 100 coins?

Asked by 5 days ago
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

Log in to vote
1
Answered by
Syclya 209 Moderation Voter
5 days ago

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
Ad
Log in to vote
1
Answered by
kisty1 7
5 days ago
Edited 5 days ago
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

Answer this question