Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't my shop script work?

Asked by 8 years ago

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

Log in to vote
1
Answered by
Voltoxus 248 Moderation Voter
8 years ago

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)
Ad

Answer this question