Hello, people of the forums. I’m having a problem with some code I’m writing.
So, I’m trying to make something like a hat giver using Gui, and it kind of works. It shows up on the character. But the problem is, it doesn’t show up on the server. Does anyone know an easy fix to this?
My code:
local button = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local rs = game:GetService("ReplicatedStorage")
button.MouseButton1Click:Connect(function()
local astronautHelmet = rs.AstronautHelmet:Clone()
if Character:FindFirstChild("AstronautHelmet") then
Character:FindFirstChild("AstronautHelmet"):Destroy()
end
if Character:FindFirstChild("Buddy Hat") then
Character:FindFirstChild("Buddy Hat"):Destroy()
end
if Character:FindFirstChild("CheeseHat") then
Character:FindFirstChild("CheeseHat"):Destroy()
end
if Character:FindFirstChild("Headphones") then
Character:FindFirstChild("Headphones"):Destroy()
end
if Character:FindFirstChild("Pot") then
Character:FindFirstChild("Pot"):Destroy()
end
if Character:FindFirstChild("TrafficCone") then
Character:FindFirstChild("TrafficCone"):Destroy()
end
astronautHelmet.Parent = Character
astronautHelmet.Handle.Motor6D.Part1 = Character.Head
end)
-- I know it's a lot, but it works for me
Like I said, if anyone has like an easy fix and can explain it to me, that would be great.
Try make a remote event in replicated storage called “GiveHat”
Then in this local script just use it to detect the players click and fire the client maybe something like this:
local button = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local giveHatEvent = rs:WaitForChild("GiveHat")
button.MouseButton1Click:Connect(function()
giveHatEvent:FireServer("AstronautHelmet")
end)
And then in ServerScriptService make a normal script like this
local rs = game:GetService("ReplicatedStorage")
local giveHatEvent = rs:WaitForChild("GiveHat")
giveHatEvent.OnServerEvent:Connect(function(player, hatName)
local character = player.Character or player.CharacterAdded:Wait()
-- Remove all hats
local hatNames = {"AstronautHelmet", "Buddy Hat", "CheeseHat", "Headphones", "Pot", "TrafficCone"}
for _, name in pairs(hatNames) do
local hat = character:FindFirstChild(name)
if hat then
hat:Destroy()
end
end
-- Clone and attach your astronaut helmet in this section
local hat = rs:FindFirstChild(hatName)
if hat then
local hatClone = hat:Clone()
hatClone.Parent = character
if hatClone:FindFirstChild("Handle") and character:FindFirstChild("Head") then
hatClone.Handle.Motor6D.Part1 = character.Head
end
end
end)
I am not sure exactly how you have it set up or why you delete all the other hats but if you follow this layout I think it should be fine