Give command nil error

I want to make /donate command and give player the time.
but stillhas “nil” error…
can u fix…?

local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(plr)
    for i,v in pairs (game.Players:GetChildren()) do
        plr.Chatted:Connect(function(msg)
            local NumberFromString = string.match(msg , "%d+")
            local timetodonate = tonumber(NumberFromString)
            if string.find(msg, "/donate")..tostring(NumberFromString) then
                    for i, v in pairs(game.Players:GetChildren()) do 
                    if string.find(msg, string.lower(v.Name)) then
                            if plr.leaderstats.Time.Value > timetodonate then
                            print(plr.Name.. " gave ".. NumberFromString.. " time to "..v.Name)
                            plr.leaderstats.Time.Value = plr.leaderstats.Time.Value - timetodonate
                            v.leaderstats.Time.Value = plr.leaderstats.Time.Value + timetodonate




end
                        end
                    end
                end
        end)
    end
end)
2 Likes

The for loop is unnecessary. Delete it and paste the contents into the PlayerAdded event

So in your code, just as @RoninQV said, the first for loop is unnecessary. Removing, the first for loop should help. Here is the modified code:
I also edited a little more so its better, this should work.

local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(plr)
        plr.Chatted:Connect(function(msg)
            local NumberFromString = string.match(msg , "%d+")
            local timetodonate = tonumber(NumberFromString)
            local message=string.lower(msg)
            if string.find(message, "/donate")..tostring(NumberFromString) then
                for i, v in pairs(game.Players:GetChildren()) do 
                    if string.find(message, string.lower(v.Name)) then
                        if plr.leaderstats.Time.Value > timetodonate then
                            print(plr.Name.. " gave ".. NumberFromString.. " time to "..v.Name)
                            plr.leaderstats.Time.Value -=timetodonate
                            v.leaderstats.Time.Value +=timetodonate
                        end
                    end
                end
            end
        end)
end)
1 Like