Tycoon Money Giver not working

I’m trying to make a silly little no effort tycoon game but its not working out how id like it too lol. I’m getting the error “attempt to index nil with WaitForChild” when i do

local leaderstats = Player:WaitForChild("leaderstats") 

code:

local Players = game:GetService("Players")

local MoneyGivers = game.Workspace:WaitForChild("MoneyGivers")

local Debounce = false

for i,v in pairs(MoneyGivers:GetChildren()) do
	local MoneyToClaim = v:FindFirstChild("MoneyToClaim")
	local LabelPart = v:FindFirstChild("LabelPart")
	local MoneyGui = LabelPart.MoneyGui
	local MoneyLabel = MoneyGui.MoneyLabel
	if v:FindFirstChild("GivePart") then
		v.GivePart.Touched:Connect(function(Hit)
			local Character = Hit.Parent
			print("Touched")
			if Character then
				if not Debounce then
					Debounce = true
					print("Touch")
					local Player = Players:GetPlayerFromCharacter(Character)
					local leaderstats = Player:WaitForChild("leaderstats")
					local Money = leaderstats.Money
					Money.Value = Money.Value + MoneyToClaim.Value
					MoneyToClaim.Value = 0
					wait(2)
					Debounce = false
				end
			end
		end)
	end
end

I have tried FindFirstChild, Player.leaderstats, nothings seemed to work. I can collect the money a couple times before i get this error.

1 Like

Is this a localscript? If so, you could just write local Player = game.Players.LocalPlayer and then do Player:WaitForChild("leaderstats") and so on.

1 Like

its a server script in ServerScriptService

1 Like

I think that you should replace your first “if then end” in the connected event by if Character:FindFirstChild("Humanoid") then .... Not putting it and just verifying if Character is not nil allows for example the part on the ground, under your instance to be considered as a Character by your code. Let me know if it fixes your problem!

3 Likes

by first if statement do you mean the if Character then?

2 Likes

this worked. thank you so much!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.