While trying to make my accessory editor for my game, the code I wrote to find and list a player’s hats ended up not functioning as intended. Here is the script in ServerScriptService.
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
if not player:HasAppearanceLoaded() then
player.CharacterAppearanceLoaded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
for _, accessory in pairs(humanoid:GetAccessories(true)) do
local hatTemplate = game:GetService("ReplicatedStorage").HatTemplate:Clone()
player.PlayerValues.AccessoryCount.Value = player.PlayerValues.AccessoryCount.Value + 1
hatTemplate.Name = accessory.Name
hatTemplate.AccessoryName.Text = accessory.Name
hatTemplate.Accessory.Value = accessory
hatTemplate.Parent = player.PlayerGui.CustomizeGUI.hatsMenu.HatsMenu.HatsDisplay
end
humanoid.Died:Connect(function()
player.PlayerValues.AccessoryCount.Value = 0
for _, accessory in pairs(humanoid:GetAccessories(true)) do
local hatTemplate = game:GetService("ReplicatedStorage").HatTemplate:Clone()
player.PlayerValues.AccessoryCount.Value = player.PlayerValues.AccessoryCount.Value + 1
hatTemplate.Name = accessory.Name
hatTemplate.AccessoryName.Text = accessory.Name
hatTemplate.Accessory.Value = accessory
hatTemplate.Parent = player.PlayerGui.CustomizeGUI.hatsMenu.HatsMenu.HatsDisplay
end
end)
end)
end)
The code functions the first time, but it does not run when the player resets. Instead the menu goes blank.
Is the ScreenGui, that the hatTemplate is parented in, has ResetOnSpawn enabled? If ResetOnSpawn is enabled, that could be why the menu doesn’t show when you reset. Try disabling it and see if that could fix the issue.
If anything, that only made the issue worse. While the accessories did show, I was unable to edit them or use them. To add on, when I pressed the “play” button the menu did not fade out as intended.
That may be something to do with your script(s) in that ScreenGui. I am not sure how that issue could be fixed, however, disabling ResetOnSpawn does fix the issue that when you reset the hatTemplate doesn’t show anymore, am I correct?
I recommend you don’t try to handle UI on the server. The server cannot track whenever the UI is updated on the client. The reason why your menu is blank is due is most likely due to that. I recommend you handle loading the different types of accessories from the client and then communicate to the server through remote events/functions to update their appearance.
--slient side (basic equipping example)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Accessories = ReplicatedStorage:WaitForChild("Accessories")
local UI = script.Parent
for _, v in pairs(Accessories:GetChildren()) do
local accessory = UI.AccessoryTemplate:Clone()
accessory.Visible = true
accessory.Parent = UI.Accessories
--update accessory details like name, etc
--from there we can interact with it on the client
accessory.Equipped.MouseButton1Click:Connect(function()
local response = ReplicatedStorage.Update:InvokeServer(v.Name) --send the server what accessory you are equipping
if response then --if equipping worked, update the button colour, etc
--update accessory details
end
end)
end
Server
--server side (basic equipping example)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Accessories = ReplicatedStorage:WaitForChild("Accessories")
ReplicatedStorage.Update.OnServerInvoke = function(player, accessory) --get player and accessory from client
--have some sort of check to see if accessory owned if needed
--put accessory on player
--update the counter you had
return true --successfully equipped accessory, send a response back it worked
end