Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't surfacegui Shop button give tools?

I wanted to make a surfacegui Shop but the points isn't subtracted and the item isn't given

local buyer = game.Players
cash = buyer:FindFirstChild("leaderstats")
local tool = game.Lighting:FindFirstChild("BloxyCola") -- put the excact weapon name here. the weapon has to be in game Lighting

function buy()
if cash ~= nil then
local points = cash:FindFirstChild("Points")
if points.Value < 5 then return end -- Cost

if points.Value >= 5 then -- Cost

local b = game.Lighting("BloxyCola"):clone() -- Tool Given

b.Parent = buyer.StarterGear

points.Value = points.Value - 5 -- Cost

end
end
end
script.Parent.MouseButton1Down:connect(buy)

Error:Workspace.Part.SurfaceGui.Button.Main:6: attempt to index global 'cash' (a nil value) Why won't it work?

1 answer

✓ Accepted answer

You have to find the players folder in players, not

game.Players

You should use a local script; Put it in StarterGui or StarterPack

local buyer = game.Players.LocalPlayer
local part = game.Workspace.Part.SurfaceGui.TextButton--obviously change the names.
cash = buyer:FindFirstChild("leaderstats")
local tool = game.Lighting:FindFirstChild("BloxyCola") -- put the excact weapon name here. the weapon has to be in game Lighting

function buy()
if cash ~= nil then
local points = cash:FindFirstChild("Points")
if points.Value < 5 then return end -- Cost

if points.Value >= 5 then -- Cost

local b = game.Lighting("BloxyCola"):clone() -- Tool Given

b.Parent = buyer.StarterGear

points.Value = points.Value - 5 -- Cost

end
end
end
part.MouseButton1Down:connect(buy)

1 comment

1

Since you refereed to the Cost(5) 3 time's, you could have used a variable for it for easier editing.

This discussion is preserved as it was. New answers, comments, and votes are closed.