Swords Not Given When Function Called

I have a script which checks for an ObjectValue called “EquippedSword” and checks its value. The value of “EquippedSword” should be a sword a player has equipped from the shop UI, I have already checked this and can confirm the value of EquippedSword is changed correctly to what sword the player equips.

The script checks for the players value of EquippedSword and then checks for the name of the value in game.ReplicatedStorage.Swords. All the spelling is correct, and the sword names match up to the EquippedSword values. However, when the function is called the sword is not cloned to the players backpack and it doesn’t return any output errors. I have also used GetAttribute instead of EquippedSword.Value, however that didn’t seem to work.

The function I’m using is as presented below;

local function giveSword()
	for _, player in pairs(Players:GetPlayers()) do
		if not player:GetAttribute("Team") then continue end

		local equippedSwordName = player.("EquippedSword").Value
		if equippedSwordName then
			local equippedSword = Swords:FindFirstChild(equippedSwordName)
			if equippedSword then
				local newSword = equippedSword:Clone()
				newSword.Name = equippedSwordName
				newSword.Parent = player.Backpack
			end
		end
	end
end

Adding on, I can confirm the function is called correctly. In summary, the sword that matches with the value is not given to the player when the function is called. Is it something to do with it being an ObjectValue? If so, can anyone offer any alternatives on what to use to store players equipped swords name? Thanks.

5 Likes

Still need help with this if anyone can.

2 Likes

The issue is you put a dot after the player in the value you gave to the equippedSwordName, instead of player.(“EquippedSword”).Value use
player[“EquippedSword”].Value or player.EquippedSword.Value.

2 Likes

Tried this, didn’t seem to work. I also used GetAttributes before and that didn’t work either.

2 Likes

mmm did u put print statements in each step of the script and check if all of them run?

2 Likes

Try debugging. Put a print after every line of code and see at which area it stops working
e.g. this but for every line

local function giveSword()
    print("test 1")
	for _, player in pairs(Players:GetPlayers()) do
        print("test 2")
		if not player:GetAttribute("Team") then continue end
        print("test 3")
		local equippedSwordName = player.("EquippedSword").Value
		if equippedSwordName then
        print("test 4")
4 Likes

i am not a good scripter but u can try this, maybe it will work

local Players = game:GetService("Players")

local function giveSword()
    for _, player in pairs(Players:GetPlayers()) do
        if not player:GetAttribute("Team") then continue end

        local equippedSwordName = player.EquippedSword.Value
        if equippedSwordName then
            local Swords = game.ReplicatedStorage.Swords
            local equippedSword = Swords:FindFirstChild(equippedSwordName)
            if equippedSword then
                local newSword = equippedSword:Clone()
                newSword.Name = equippedSwordName
                newSword.Parent = player.Backpack
            end
        end
    end
end

giveSword()
3 Likes

Chances are the problem is right here

local equippedSword = Swords:FindFirstChild(equippedSwordName)

It doesn’t return any error because you are checking if it found the sword, and it didn’t, thus, doing nothing as you don’t have an else function for if equippedSword then to detect if it didn’t find the sword. A few guesses I can think of is that you didn’t update the sword name on the server-side. Though to make sure just add an else function to see the name of the equippedSword

so change this

			if equippedSword then
				local newSword = equippedSword:Clone()
				newSword.Name = equippedSwordName
				newSword.Parent = player.Backpack
			end

to this

			if equippedSword then
				local newSword = equippedSword:Clone()
				newSword.Name = equippedSwordName
				newSword.Parent = player.Backpack
			else
				print(equippedSword)
			end

Note this wont fix your code, this’ll just help you find what the problem is

4 Likes

If you’re using a type of value instance, try this instead:

local equippedSwordName = player:FindFirstChild("EquippedSword")
if equippedSwordName then
    local equippedSword = Swords:FindFirstChild(equippedSwordName.Value)

I have this exact code in my game (in a different way obviously as I’m not using swords) and it works without any errors.

Edit: Didn’t notice it replied to Sniper. Apologies.

3 Likes

make sure that you are changing the attribute of the player via a serverscript or otherwise it will skip when you are doing the if not player:GetAttribute("Team") then continue end

2 Likes

Just debugged, it returns “nil” when I ask it to print the name of the corresponding sword within ReplicatesStorage (sorry for sending pic of code instead of text, currently on mobile).


1 Like

try this.

local function giveSword()
    print("test 1")
    for _, player in pairs(Players:GetPlayers()) do
        print("test 2")
        if not player:GetAttribute("Team") then
            continue
        end
        print("test 3")
        local equippedSwordValue = player:FindFirstChild("EquippedSword")
        if equippedSwordValue then
            local equippedSwordName = equippedSwordValue.Value
            print("Equipped Sword Name:", equippedSwordName)
            print("test 4")
        else
            print("No equipped sword found for player:", player.Name)
        end
    end
end

1 Like

Didn’t seem to work out, just returns a blank message when showing the equipped sword name.

1 Like

First make sure you are using this services

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Now it looks like the issue might be with how you’re accessing the EquippedSword value and finding the corresponding sword object in ReplicatedStorage.Swords so make a few adjustments to your script for issues.

local function giveSword()
    for _, player in pairs(Players:GetPlayers()) do
        if not player:GetAttribute("Team") then continue end

        local equippedSwordName = player:WaitForChild("EquippedSword").Value
        if equippedSwordName then
            local swordFolder = ReplicatedStorage:WaitForChild("Swords")
            local equippedSword = swordFolder:FindFirstChild(equippedSwordName)
            if equippedSword then
                local newSword = equippedSword:Clone()
                newSword.Parent = player.Backpack
            else
                warn("No sword found with name:", equippedSwordName)
            end
        else
            warn("EquippedSword value not found for player:", player.Name)
        end
    end
end

Lmk if output warns one of these

1 Like

Output warns “No sword found with name:” and it’s just blank after, I’m starting to think it’s not able to get the text inside of the “Value” of the “EquippedSword” since it is an “ObjectValue”? Could this be why? Any alternatives I should use for storing the equipped sword name instead of a ObjectValue? Thanks.

1 Like

Yes, you’re correct. Since EquippedSword is an ObjectValue, accessing its Value property directly may not work as expected. Instead, you can store the equipped sword’s name as a string value directly in the ObjectValue!

1 Like

My mistake, it was actually a StringValue this whole time. I changed my shop script so it changes the StringValue’s Name instead of the Value and that worked.

The name changes when a sword is equipped and I have checked to make sure that it changes properly, and it does. However, the function says “No sword found with name: EquippedSword”. “EquippedSword” is the original name of the StringValue and it gets changed when a player equips answord, the StringValue name was not “EquippedSword” when I called the function yet the script still thinks its called “EquippedSword” when it’s actually called “Steampunk Sword”? (I checked this within the explorer and children of the player)

1 Like

Do you change or set value of StringValue Player.EquippedSword from a LocalScript or ServerScript?

It’s changed inside of a LocalScript.

And the code above is from server?