I am trying to make a script that adds money every 60 seconds of you playing the game to a leaderstat I already created, but it doesn't seem to work at all, all I saw was this error: attempt to index nil with 'FindFirstChildWhichIsA', any fixes? There doesn't seem to be any syntax errors in my code. Heres a code snippet.
local stats = game.Players.LocalPlayer:FindFirstChildWhichIsA("Folder") while true do wait(60) if stats.Name == "leaderstats" then stats.Money.Value = stats.Money.Value + 50 print("Gave"..game.Players.LocalPlayer.Name" money!") else print("Couldn't find leaderstats folder in "..game.Players.LocalPlayer.Name) end end
I think your problem is you are trying to define player by LocalPlayer inside a server script, which wont work. game.Players.LocalPlayer is meant to define the local player on a client, which the server doesn't have access to. Instead use a server script and loop through all the players every 60 seconds adding cash to each one
while true do -- Loop wait(60) -- Wait 60 seconds for _,Player in pairs(game.Players:GetChildren()) do -- Iterate through each player, running below code on each one local lstats = Player:FindFirstChild("leaderstats") -- Find leaderstats if lstats ~= nil then -- If leaderstats found then lstats.Money.Value = lstats.Money.Value + 50 -- Add some money end end end
Here are some ways to get the player via server scripts
game.Players.PlayerAdded:Connect(function(player) -- This function will run for each player when they join the server, you can then run some code like below print(player) end --You can get a player from remote events fired from a local script local function TestFunction(player) print(player) end game.ReplicatedStorage.Remotes.TestRemote.OnServerConnect(TestFunction) -- You can get players from click detectors function onMouseClick(player) print(player.. " clicked me!") end part.ClickDetector.MouseClick:Connect(onMouseClick)