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

How to fix this script so that the player can only buy it once?

Asked by 6 years ago
Edited 6 years ago

Annyeong!Hello!

So I have a shop gui,then there is a buy button,what i want it to do is that i want the player to buy a tool,then the player will not be able to buy it again.It will cost the in game currency,so i do not want it to be in a games form.However it doesn't work.I don't know what is wrong but i tried :FindFirstChild and still did not work

There was also NO output error


Local Script:

local plr = game.Players.LocalPlayer
local Cloud = game.ReplicatedStorage["Flying Cloud"]
local button = script.Parent


button.MouseButton1Click:Connect(function()
    if plr.leaderstats.Scikoins.Value == 100 or plr.leaderstats.Scikoins.Value >= 100 then
plr.leaderstats.Scikoins.Value = plr.leaderstats.Scikoins.Value - 100       
Cloud:Clone().Parent = plr.Backpack
        wait(2)
        warn(plr.Name.."has bought a cloud")
        else
        warn(plr.Name.."has not enough scikoins")
        if plr.Backpack:FindFirstChild("Flying Cloud") then
            warn(plr.Name.."already has a cloud")
        end
    end
end)
0
Maybe make it so it runs the same thing when the player has no scikoins? exarlus 72 — 6y
0
hm ok... KawaiiX_Jimin 54 — 6y
0
@exarlus i don't get it tho KawaiiX_Jimin 54 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

The logic to your script is wrong. Look at your last if statement, it's inside the else. This means that if statement can only run if plr does not have enough money AND already has the item.

    local plr = game.Players.LocalPlayer
    local Cloud = game.ReplicatedStorage["Flying Cloud"]
    local button = script.Parent

    button.MouseButton1Click:Connect(function()
    if plr.leaderstats.Scikoins.Value == 100 or plr.leaderstats.Scikoins.Value >= 100 then
        if plr.Backpack:FindFirstChild("Flying Cloud") then
            warn(plr.Name.."already has a cloud")
        else
            plr.leaderstats.Scikoins.Value = plr.leaderstats.Scikoins.Value - 100      
            Cloud:Clone().Parent = plr.Backpack
            wait(2)
            warn(plr.Name.."has bought a cloud")
        end     
    else
        warn(plr.Name.."has not enough scikoins")       
    end
    end)

Remember that your code checks from top to bottom, and left to right. I thinks that's the main problem (I didn't check it though).

Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
local plr = game.Players.LocalPlayer
local Cloud = game.ReplicatedStorage["Flying Cloud"]
local button = script.Parent


button.MouseButton1Click:Connect(function()

   if plr.Backpack:FindFirstChild("Flying Cloud") then
            warn(plr.Name.."already has a cloud")
        return -- returns the function
        end

    if plr.leaderstats.Scikoins.Value == 100 or plr.leaderstats.Scikoins.Value >= 100 then
plr.leaderstats.Scikoins.Value = plr.leaderstats.Scikoins.Value - 100       
Cloud:Clone().Parent = plr.Backpack
        wait(2)
        warn(plr.Name.."has bought a cloud")
        else
        warn(plr.Name.."has not enough scikoins")
       -- if plr.Backpack:FindFirstChild("Flying Cloud") then -- switch this so it is above the buy system and the script reads it first before subtracting money from the player
          --  warn(plr.Name.."already has a cloud")
       -- end
    end
end)

Answer this question