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

How to stop cloning after item is already given?

Asked by 6 years ago
Edited 6 years ago

Well, its been a while since I scripted and now I'm rusty (not that I was any good before). I tried making a simple script that gives me an item in RepilcatedStorage when I press a TextButton. I successfully did it. I added a currency so I could deplete the points a character had if it decided to get a sword which cost 100 points. However, when I tried to tell the script to stop giving it to me once there was was already one in the backpack, it wouldn't work. I tried creating an if statement and I don't think I made it correctly. So here's the script I think will fix it. Did I do it right? Is there anything I can do to make my code cleaner?

Keep in mind the Item I'm cloning is in ReplicatedStorage and this local script is a child to the TextButton.

script.Parent.MouseButton1Click:connect(function()
    local player = game.Players.LocalPlayer
    local sword = game.ReplicatedStorage.ClassicSword
    local points = player.leaderstats.Points.Value
    local bought = false
    if points.Value > 0 then
        local newsword = sword:Clone()
        newsword.Parent = player.Backpack
        local bought = true
    else
        print("Already owned or insufficient funds")
        bought = true
    end 
end)
0
debounce? Lava_Scripter 109 — 6y
0
doesn't the bought variable serve as a debounce already? Mariojumper5 26 — 6y

1 answer

Log in to vote
4
Answered by 6 years ago
Edited 6 years ago

You just need to add a couple of checks:

--move these to the beginning so you arent running them everytime the event fires

local player = game.Players.LocalPlayer
local sword = game:GetService("ReplicatedStorage"):WaitForChild("ClassicSword")
local points = player:WaitForChild("leaderstats"):WaitForChild("Points") --omitted value property

script.Parent.MouseButton1Click:connect(function()
    if points.Value > 0 and player.Backpack:FindFirstChild(sword.Name) == nil and player.Character:FindFirstChildOfClass("Tool") == nil then

     --the first one i added checks the backpack, and the second makes sure a tool isnt in the character (where tools go when equipped)

        local newsword = sword:Clone()
        newsword.Parent = player.Backpack
    else
        print("Already owned or insufficient funds")
    end 
end)

You might put those two checks in a new line, since it looks kind of messy.


Accept and upvote if this helps!

0
Well It didn't work for some reason. Also, My bad I forgot to add the line where it subtracts points(-100 each time). Of course I'm wondering why you used :WaitForChild() so many times. Of course the reason its not working is probably because of the conditions we set in the if statement. I don't really know how to fix this. Help me solve this little issue of mine then i'll accept and upvote :v Mariojumper5 26 — 6y
0
my bad, i misused "or" when i should have used "and". the wait for childs are necessary to make sure those objects exist Gey4Jesus69 2705 — 6y
Ad

Answer this question