hi, I’m making a class system, and I need the hat of the npc to change locally, the hat itself is cloned and appears in the npc, but it’s not in its place, I tried to fix it, but I couldn’t do anything about it
a whole local script:
local RS = game:GetService("ReplicatedStorage")
local Module = require(RS:WaitForChild("ClassesModule"))
local player = game.Players.LocalPlayer
local Remotes = RS:WaitForChild("Remotes")
local buttons = script.Parent.Classes.ScrollingFrame:GetDescendants()
local playerData = Remotes.GetPlayerClasses:InvokeServer()
local equippedClass = player:FindFirstChild("ClassEquipped") and player.ClassEquipped.Value or nil
wait(2)
local npc = workspace:FindFirstChild("NpcClasses")
local function updateButtonText(className)
local BuyButton = script.Parent.Stats:FindFirstChild("Button")
if BuyButton then
if playerData[className] then
if equippedClass == className then
BuyButton.Text = "UNEQUIP"
else
BuyButton.Text = "EQUIP"
end
else
BuyButton.Text = "BUY"
end
end
end
-- That function answers for npc hats
local function changeNPCHat(className)
if not npc then return end
for _, accessory in ipairs(npc:GetChildren()) do
if accessory:IsA("Accessory") and RS.HatsClasses:FindFirstChild(accessory.Name) then
accessory:Destroy()
end
end
local classAccessory = RS.HatsClasses:FindFirstChild(className)
if classAccessory then
local accessoryClone = classAccessory:Clone()
accessoryClone.Parent = npc
local attachmentPart = npc:FindFirstChild("Head") or npc.PrimaryPart
if attachmentPart and accessoryClone:FindFirstChild("Handle") then
local attachment = accessoryClone.Handle:FindFirstChildOfClass("Attachment")
if attachment then
local npcAttachment = attachmentPart:FindFirstChild(attachment.Name)
if npcAttachment then
accessoryClone.Handle.CFrame = npcAttachment.CFrame
end
end
end
end
for _, button in ipairs(buttons) do
if button:IsA("TextButton") then
local titleLabel = script.Parent.Stats:FindFirstChild("ClassName")
local descriptionLabel = script.Parent.Stats:FindFirstChild("Description")
local PriceLabel = script.Parent.Stats:FindFirstChild("Price")
local BuyButton = script.Parent.Stats:FindFirstChild("Button")
button.MouseButton1Click:Connect(function()
local className = button.Name
for _, classData in ipairs(Module) do
if classData.Name == className then
changeNPCHat(className)
titleLabel.Text = classData.Name
descriptionLabel.Text = classData.Description
PriceLabel.Text = classData.Price
script.Parent.ClassSelected.Value = className
updateButtonText(className)
break
end
end
end)
end
end
script.Parent.ClassSelected:GetPropertyChangedSignal("Value"):Connect(function()
local className = script.Parent.ClassSelected.Value
updateButtonText(className)
end)
local className = script.Parent.ClassSelected.Value
updateButtonText(className)
script.Parent.Stats.Button.MouseButton1Click:Connect(function()
local className = script.Parent.ClassSelected.Value
local BuyButton = script.Parent.Stats.Button
if BuyButton.Text == "BUY" then
for _, classData in ipairs(Module) do
if classData.Name == className then
if player.leaderstats.Cash.Value >= classData.Price then
player.leaderstats.Cash.Value -= classData.Price
Remotes.ClassBuy:FireServer(className)
playerData[className] = true
BuyButton.Text = "EQUIP"
updateButtonText(className)
end
break
end
end
elseif BuyButton.Text == "EQUIP" then
Remotes.EquipClass:FireServer(className, true)
equippedClass = className
elseif BuyButton.Text == "UNEQUIP" then
Remotes.EquipClass:FireServer(className, false)
equippedClass = nil
end
updateButtonText(className)
end)