Hello! I am using Player Store for this, and the problem is that the GetWeapons function is returning nil.
Players.PlayerAdded:Connect(function(player)
print(player.Name)
local playerWeapons = DataManager.GetWeapons(player)
if playerWeapons then
GetPlayerWeapons:FireClient(player, playerWeapons)
else
print(player.Name .. " does not have any weapons.")
end
end)
The script inside module script
function DataManager.GetWeapons(player: Players)
local profile = DataManager.Profiles[player]
if not profile then return end
local weapons = profile.Data.Weapons
return weapons
end
I used the function here and it works fine:
local playerItemList = DataManager.GetWeapons(player)
if table.find(playerItemList, UniqueID) then
warn("Player already owns this weapon")
return
end
Has their profile loaded by the time you call the function? I notice that the function fails on PlayerAdded, which fires instantly after the player joins the server. Maybe you need to wait until their data loads before calling it?
I assume you handle the data loading in another script, in there you can set up a signal that fires when the data of a player is loaded, and listen to that instead of PlayerAdded in this script.
GetWeapons returns nil because the player’s profile isn’t loaded yet when you call it. You need to wait until DataManager.Profiles[player] exists before calling GetWeapons, For example:
Players.PlayerAdded:Connect(function(player)
local profile = DataManager.Profiles[player]
while not profile do
task.wait(0.1)
profile = DataManager.Profiles[player]
end
local playerWeapons = DataManager.GetWeapons(player)
if playerWeapons then
GetPlayerWeapons:FireClient(player, playerWeapons)
else
print(player.Name .. " does not have any weapons.")
end
end)
Make sure the keys in DataManager.Profiles are player instances, not user IDs or other types.
Thank you so much ! I have another question, If I’m setting a boolean attribute to true inside a local script and then checking in another local script if button:GetAttribute(“Owns”) == true then return end, will it return end?
No, setting an attribute in one LocalScript doesn’t automatically sync to another LocalScript unless both run on the same client and the attribute was set before the check. Attributes set in a LocalScript affect only the client’s local copy of the instance. If the second LocalScript runs before the attribute is set, it won’t see true. For syncing across clients or server, you need RemoteEvents or server-set attributes.
So, my system works like this: once a player joins, it loads their data, and if the player owns weapons, it will change the text on the buttons and set the “Owns” attribute to true (Client-Sided) .
I tried checking it like this: if button and button:GetAttribute("Owns") ~= true then, but the script still continues. I just want to make it so that if the player owns the weapon, the rest of the script won’t execute. So, I can handle weapon equipping
Local Script
for _, weapon in pairs(WeaponListFrame:GetChildren()) do
local button = weapon:FindFirstChildOfClass("TextButton")
if button and button:GetAttribute("Owns") ~= true then
local UniqueID = button:GetAttribute("UniqueID")
button.MouseButton1Click:Connect(function()
if UniqueID then
ShopPurchaseEvent:FireServer(UniqueID)
end
end)
end
end
The issue is that your attribute check happens when the script runs, but the Owns attribute might not be set yet or synced properly on the client at that time. So your if button:GetAttribute("Owns") ~= true then condition lets the script continue anyway.
What you want is to check the Owns attribute right when the button is clicked, not just once during setup. Something like this:
button.MouseButton1Click:Connect(function()
if button:GetAttribute("Owns") == true then return end
local UniqueID = button:GetAttribute("UniqueID")
if UniqueID then
ShopPurchaseEvent:FireServer(UniqueID)
end
end)
This way, if the player owns the weapon, the click handler just exits early and won’t fire the purchase event.
Also, make sure you set the Owns attribute before the UI initializes, and ideally sync it from the server — client-only attributes can cause timing issues.