I have a setup where if you press a button on a GUI it gives you a hat but if you press it more than once it gives you it multiple times
Problem is same hat being cloned pressing button on GUI
Server Script
local chooseHats = game.ReplicatedStorage:WaitForChild("ChooseHats")
local hatsFold = game.ReplicatedStorage:WaitForChild("Accessories")
chooseHats.OnServerEvent:Connect(function(player,fullName)
local character = player.Character
local accessory = hatsFold:FindFirstChild(fullName)
if accessory ~= nil then
local cloneHat = accessory:Clone()
cloneHat.Parent = character
else
accessory:Destroy()
end
end)
Local Script:
local chooseHats = game.ReplicatedStorage:WaitForChild("ChooseHats")
local hatsFold = game.ReplicatedStorage:WaitForChild("Accessories")
local player = game.Players.LocalPlayer
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("ImageButton") or v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
local fullName = v:WaitForChild("Accessory").Value
chooseHats:FireServer(fullName,player)
end)
end
end
I would assume it is because every time you are clicking the button, the script is cloning the Accessory instance into your character, and there is probably no checks to see if the Player in game is already wearing that hat. Thus why it gives you the same hat multiple times.
Try adding something like a bool value, and make the value True whenever the player clicks. use an if statement to check if its true, if its false, then clone it. If its true, then print something.
if not player.Character:FindFirstChild(hat.Name) then --make the hat inside the brackets the name of the hat
--clone it
else
print("playerhas hat already")
end
ok im trying to make it so any hairs they choose get destroyed because i only want it so they can wear one, but i am struggling so bad maybe im tired and not too educated on it but yeah this script doesn’t work
local hairsFold = game.ReplicatedStorage:WaitForChild("Hairs")
ChooseHairs.OnServerEvent:Connect(function(player,fullName)
local character = player.Character
local accessory = hairsFold:FindFirstChild(fullName)
local wearing = true
if not player.Character:FindFirstChild("Accessory").Wearing == false then
local cloneHair = accessory:Clone()
cloneHair.Parent = character
wearing = true
end
end)
I was trying to put string values into the accessories to try and see if I can make it so you can only wear one type of accessory with that value in it
Don’t be too stressed out! I’ll help you understand this code:
local hairsFold = game.ReplicatedStorage:WaitForChild("Hairs") -- The folder of hairs
ChooseHairs.OnServerEvent:Connect(function(player, fullName)
local character = player.Character or player.CharacterAdded:Wait() -- Getting the player's character
local accessory = hairsFold:FindFirstChild(fullName) or nil -- So basically it will return the accessory if it's found or it will return nil if there's no hair named as it
if not player:FindFirstChildOfClass("Accessory"):FindFirstChild("HairValue") then
if accessory == nil then return end -- It'll return if the accessory is nil
accessory = accessory:Clone() -- Changing the accessory variable to be a clone of itself
local HairValue = Instance.new("BoolValue")
HairValue.Name = "HairValue"
HairValue.Parent = accessory -- Creating a new object named 'HairValue' so that it can be recognized as one of the hairs the player chose
accessory.Parent = character -- Parenting the accessory to the character
end
end)
Tell me if you don’t understand anything or if it isn’t working!
I can understand, but it doesn’t work because at this line- if not player:FindFirstChildOfClass("Accessory"):FindFirstChild("HairValue") then
There is a local script is inside a GUI button and they connect with a remote event, and inside the local script which I put above, it uses the value name inside the GUI button called “Hair” and that value is the accessory name that I want the player to wear.
local accessory = hairsFold:FindFirstChild(fullName)
if not player.Character:FindFirstChild(accessory.Name) then
That works because “fullName” is the accessory name inside the local script, it’s the StringValue’s name so when you click the button it would give you the hair, I just can’t figure it out so a player can only wear one type of that accessory by using a value but my brain is mushy and I can’t figure it out
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("ImageButton") or v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
local fullName = v:WaitForChild("Hair").Value
chooseHairs:FireServer(fullName,player)
end)
end
end
tl;dr, i guess i just need to figure out the boolvalue part in the accessory I don’t want people in my game to be able to wear more than one hair, it would get too messy