hi, I’m making classes for my game, and I need the class hats to be shown on the NPC, that is, how this hat will look on the player, and I can’t write it in a local script, I tried to implement it using RemoteEvent, but nothing really helped.
Here is the local script itself:
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
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
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)
once again, I tried to fix it via RemoteEvent, but the event wasn’t sent to the client, so now I’m trying to do it exclusively on the client side.