Sword giver not always giving sword

So basically, my sword giver is supposed to give the player what sword they have chosen. Right now, it works, but it doesn’t always give the sword

It’s caused by the admin we are using. It has a client loader placed in the player’s backpack that messes with the code, how would I filter for this script (it has no name) but keep the same functionality?

Edit: The admin script is not always there, it’s there only half of the time.*
Edit 2: Re-wrote post, I was asking about a different problem**

Code:

script.Parent.Touched:Connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
    if plr then
        local swordName = game.ServerStorage.EquipedSwords:FindFirstChild(plr.Name).Value
        if #plr.Backpack:GetChildren() < 1 then
            for _, sword in ipairs(game.ServerStorage.Swords:GetDescendants()) do
                if sword.Name == swordName then
                    local clone = sword:Clone()
                    clone.Parent = plr.Backpack
                end
            end
        end
    end
end)

Thanks in advance. :grinning:

I would suggest a debounce, but also this system could be bypassed if the player has a tool already equipped (you aren’t checking their character).

But the reason why it gives them multiple swords is because It’s literally looping through all of ServerStorage.Swords.

If you only want a certain sword from there, you should just do this:

script.Parent.Touched:Connect(function(part)
	local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	if plr then
		local swordName = game.ServerStorage.EquipedSwords:FindFirstChild(plr.Name).Value
		if #plr.Backpack:GetChildren() < 1 then
			local sword = game.ServerStorage.Swords["EpicSword"]:Clone()
			sword.Parent = plr.Backpack
		end
	end
end)

The folder has multiple folders in each of them, that’s why I loop through all of it.

Ah I see, my apologies. Try something like this (please make sure to edit it though, as I have no way of testing it):

local ServerStorage = game:GetService("ServerStorage")

local function alreadyOwnsSword(player, swordName)
	if #player.Backpack:GetChildren() > 1 then
		return true
	end
	if player.Character:FindFirstChild(swordName) then
		return true
	end
end

script.Parent.Touched:Connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	if not plr then return end
	
	local swordName = ServerStorage.EquippedSwords[plr.Name].Value
	
	if alreadyOwnsSword(plr, swordName) then return end
	
	for _, sword in ipairs(ServerStorage.Swords:GetDescendants()) do
		if sword.Name == swordName then
			local clone = sword:Clone()
			clone.Parent = plr.Backpack
			return
		end
	end
end)

Actually, I’ve verified this system works:

local ServerStorage = game:GetService("ServerStorage")

local function alreadyOwnsSword(player, swordName)
	if player.Backpack:FindFirstChild(swordName) then
		return true
	end
	if player.Character:FindFirstChild(swordName) then
		return true
	end
end

script.Parent.Touched:Connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	if not plr then return end
	
	local swordName = ServerStorage.EquippedSwords[plr.Name].Value
	
	if alreadyOwnsSword(plr, swordName) then return end
	
	for _, sword in ipairs(ServerStorage.Swords:GetDescendants()) do
		if sword.Name == swordName then
			local clone = sword:Clone()
			clone.Parent = plr.Backpack
			return
		end
	end
end)

The issue has to do with your backpack check, I think this should work a lot better for your use case.

2 Likes