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.
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.
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")
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()
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
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.
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
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).
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
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
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.
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!
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)