Basically I want to click on a model that'll then dissapear and give you money yet I get the error saying taht they're trying to attempt the field "leaderstats" a nil value, here is the script
function OnClick()
local player = game.Players:GetPlayers() for i=1,#player do if (player[i]:FindFirstChild("leaderstats") ~= nil) then local clicks = player[i].leaderstats.Money if (clicks ~= nil) then player.leaderstats.Money.Value = player.leaderstats.Money.Value + 250 script.Parent.Box.CanCollide = false script.Parent.Box.Transparency = 1 script.Parent.TV3.CanCollide = false --Change COUCH to the name of your model! script.Parent.TV3.Transparency = 1 -- same here wait(60) -- Change this to the amount of time you want until it respawns (can be any number) script.Parent.Box.CanCollide = true script.Parent.Box.Transparency = 1 script.Parent.TV3.CanCollide = true -- change COUCH to the name of your model ! script.Parent.TV3.Transparency = 0 -- same here end end end end script.Parent.ClickDetector.MouseClick:connect(OnClick)
The reason you're getting that error is because the variable "player" is defined as a table of all the players in the server, not just a single player. You already have a variable that is defined as the players money, so you can replace line 13 with:
clicks.Value = clicks.Value + 250
After fixing this I believe that your code rewards every player in the server with money, not just the player who clicked the model which I believe this is not what you want. To fix this you can recreate your function with a "playerWhoClicked" parameter instead. For example:
local function OnClick(player) local money = player.leaderstats.Money money.Value = money.Value + 250 local objects = script.Parent:GetChildren() for i = 1, #objects do local object = objects[i] if object:IsA("BasePart") then object.Transparency = 1 object.CanCollide = false end end wait(60) for i = 1, #objects do local object = objects[i] if object:IsA("BasePart") then object.Transparency = 0 object.CanCollide = true end end end script.Parent.ClickDetector.MouseClick:Connect(OnClick)
For other information about ClickDetector.MouseClick, you can visit this page: https://developer.roblox.com/api-reference/event/ClickDetector/MouseClick