Error:15:18:16.560 - Players.Player1.PlayerGui.MainShop.MainShop.Frame.Assault R:19: attempt to index global 'GuiButtonPath' (a nil value)
Full script: vvvvvv
local player = game.Players.LocalPlayer --This only works in a local script. cost = 0 --assign the cost of the AssaultRifle here. function checkForAssaultRifle() for i, v in pairs(player.Backpack:GetChildren()) do if v.Name == "AssaultRifle" then return true end end for i, v in pairs(player.Character:GetChildren()) do if v.Name == "AssaultRifle" then return true end end return false end GuiButtonPath.MouseButton1Click:connect(function() local needsAssaultRifle = not checkForAssaultRifle() --needsAssaultRifle is the opposite of found. local leaderstats = player:FindFirstChild("leaderstats") if leaderstats ~= nil then local Money = leaderstats:FindFirstchild("Money") if Money ~= nil then if Money.Value >= cost and needsAssaultRifle then --if they have the money and they need a AssaultRifle Money.Value = Money.Value - cost --subtract cost from funds AssaultRifle = game.Lighting.AssaultRifle:Clone() AssaultRifle.Parent = player.Backpack --give them a AssaultRifle end end end end)
It was working earlier, and I know the cost isn't the problem, it just stopped working after I change the previous items name with 'AssaultRifle'. Thanks!
The problem is that you are returning it wrong. Use this way instead, it's hard to explain clearly. Hopefully someone will be able to put it into words.
local player = game.Players.LocalPlayer --This only works in a local script. cost = 0 --assign the cost of the AssaultRifle here. function checkForAssaultRifle() for i, v in pairs(player.Backpack:GetChildren()) do if v.Name == "AssaultRifle" then return true end end for i, v in pairs(player.Character:GetChildren()) do if v.Name == "AssaultRifle" then return true end end return false end GuiButtonPath.MouseButton1Click:connect(function() local needsAssaultRifle = not checkForAssaultRifle() --needsAssaultRifle is the opposite of found. local leaderstats = player:FindFirstChild("leaderstats") if leaderstats ~= nil then local Money = leaderstats:FindFirstchild("Money") if Money ~= nil then if Money.Value >= cost and needsAssaultRifle then --if they have the money and they need a AssaultRifle Money.Value = Money.Value - cost --subtract cost from funds AssaultRifle = game.Lighting.AssaultRifle:Clone() AssaultRifle.Parent = player.Backpack --give them a AssaultRifle end end end end)
Make sure you have GuiButtonPath
as a variable in the script, or the script will have no idea what you're talking about. Do something like this:
local GuiButtonPath = script.Parent.GuiButtonPath
If you don't have GuiButtonPath
as a variable in the script, the script doesn't have a way of identifying what GuiButtonPath
is