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