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 my shop script work?

I have a localscript that is supposed to give a player a sword if they click an ImageButton, but it won't work. Does anyone know why and can you help me fix it?

local ItemName = "Orc Blade"
local Cost = 100
local Currency = "Coins"

MinusMoney = true
SaveOnDeath = true

local player = game.Players.LocalPlayer
local ToolClone = game.Lighting:findFirstChild(ItemName):clone()

script.Parent.MouseButton1Click:connect(function(player)
	if player.leaderstats:findFirstChild(Currency) ~= nil then
		local cash = player.leaderstats:findFirstChild(Currency)
		if cash.Value >= Cost and player.Backpack:findFirstChild(ItemName) == nil then
			local ToolClone2 = ToolClone:clone()
			ToolClone2.Parent = player.Backpack
			if MinusMoney == true then
				cash.Value = cash.Value - Cost
			end
			if SaveOnDeath == true then
				local ToolClone3 = ToolClone:clone()
				ToolClone3.Parent = player.StarterGear
			end
		end
	end
end)

1 answer

✓ Accepted answer

Its pretty weird, but what I assume is happening which doesn't make any sense is local player isn't loading even thought you pre-declared it. To fix this I moved the player declaration to inside the function.

local ItemName = "Orc Blade"
local Cost = 100
local Currency = "Coins"

MinusMoney = true
SaveOnDeath = true

local ToolClone = game.Lighting:findFirstChild(ItemName):clone()

script.Parent.MouseButton1Click:connect(function(player)
	local player = game.Players.LocalPlayer
    if player.leaderstats:findFirstChild(Currency) ~= nil then
        local cash = player.leaderstats:findFirstChild(Currency)
        if cash.Value >= Cost and player.Backpack:findFirstChild(ItemName) == nil then
            local ToolClone2 = ToolClone:clone()
            ToolClone2.Parent = player.Backpack
            if MinusMoney == true then
                cash.Value = cash.Value - Cost
            end
            if SaveOnDeath == true then
                local ToolClone3 = ToolClone:clone()
                ToolClone3.Parent = player.StarterGear
            end
        end
    end
end)

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