Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Have problem with my Gui Shop! SOS?

function CheckSword()
	found = false
	for i, v in pairs(Player.Backpack:GetChildren()) do
		if v.Name == "Sword" then
			found = true
		end
		if found == true then
			return 
			
		end
	end
end

Okay, so I'm trying to make it so that the player can only buy one sword, so if he or she has the sword in their backpack they will be denied. So this is the full script.

Player = game.Players.LocalPlayer
Price = 50
function CheckSword()
	found = false
	for i, v in pairs(Player.Backpack:GetChildren()) do
		if v.Name == "Sword" then
			found = true
		end
		if found == true then
			return 
			
		end
	end
end
script.Parent.MouseButton1Click:connect(function()
	local leaderstats = Player:FindFirstChild("leaderstats")
	if leaderstats ~= nil then
		local Gold = leaderstats:FindFirstChild("Points")
		if Gold ~= nil then
			SwordFound = CheckSword()
			if Gold.Value > Price and SwordFound == false then
				Sword = game.Lighting.Sword:Clone()
				Sword.Parent = game.Players[Player.Character.Name].Backpack
			end
		end
	end
end)

But when I click "Buy", nothing happens. This started happening after I added the, for i, v in pairs part..

Any soloutions?

1 answer

First, make sure this script is a Local Script! Before I do anything, I'll let you know that the second a player equips a tool, it goes from backpack to their character so this makes it possible for them to gain a second sword if they equip while buying. To combat this, we want to check their character AND backpack!

Also, I think you're returning wrong as well.

Check bellow for a fixed script with some commenting on problems. I Changed up some functionality of your script, It seemed easier.

Player = game.Players.LocalPlayer
Price = 50

script.Parent.MouseButton1Click:connect(function()
    local leaderstats = Player:FindFirstChild("leaderstats")
    if leaderstats ~= nil then
        local Gold = leaderstats:FindFirstChild("Points")
        if Gold ~= nil then
            if Gold.Value >= Price then --Wouldn't you want to buy something if you had the exact amount of money as the price?
		if Player.Backpack:FindFirstChild("Sword") == nil and Player.Character:FindFirstChild("Sword") == nil then -- This will make sure they have no sword in their character or backpack, if so, continue the code.
		           Sword = game.Lighting.Sword:Clone()
		           Sword.Parent = game.Players[Player.Character.Name].Backpack
		   end
            end
        end
    end
end)

if this helped, accept this answer and toss any questions my way! Any errors? Just tell them to me and I'll fix it for you!

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