✓ Accepted answer
Local Script
local Market = game:GetService("MarketplaceService")
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("AttachHat")
local Player = game.Players.LocalPlayer
local ButtonGui = script.Parent
local Gui = ButtonGui.Parent:WaitForChild("HatGui")
ButtonGui.TextButton.Activated:Connect(function()
if Market:UserOwnsGamePassAsync(Player.UserId, 5949403) then
Gui.Enabled = true
ButtonGui.Enabled = false
else
Gui.Enabled = false
ButtonGui.Enabled = true
end
end)
for i, v in pairs(Gui:GetDescendants()) do
if v:IsA("ImageButton") then
v.Activated:Connect(function()
Remote:FireServer(v.Name)
end)
end
end
Server Script
local Remote = Instance.new("RemoteEvent")
Remote.Name = "AttachHat"
Remote.Parent = game:GetService("ReplicatedStorage")
Remote.OnServerEvent:Connect(function(Player, Button)
for i, v in pairs(Player.Characer:GetDescendants()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
local newaccessory = Instance.new("Accessory")
newaccessory.Parent = Player.Character
local newhandle = Instance.new("Part")
newhandle.Parent = newaccessory
newhandle.Size = Vector3.new(1, 1, 1)
newhandle.CanCollide = false
newhandle.Anchored = false
local newmesh = Instance.new("SpecialMesh")
newmesh.Parent = newhandle
newmesh.Scale = Vector3.new(1, 1, 1)
local newweld = Instance.new("Weld")
newweld.Part1 = Player.Character:WaitForChild("Head")
newweld.Part0 = newhandle
if Button == "Button1" then
newweld.C0 = CFrame.new(0, 0, 0)
newmesh.MeshId = "http://www.roblox.com/asset/?id=0"
newmesh.TextureId = "http://www.roblox.com/asset/?id=0"
end
end)
UserOwnsGamePassAsync is used to get the GamepassId, UserHassPass is deprecated
You need to use Remote Events (one is created in the Server Script for you) in order to communicate between the Server and the Client
In this example, there are two ScreenGuis (one for the button to open the hat gui)
Place your TextButton / LocalScript into the "ButtonGui" and the ImageButtons into the "HatGui"
If you add Frames, make sure to account for these in the definitions of Gui and ButtonGui
The thing to edit for each button is to make sure that the name of the button matches a string as shown by if (Button = "Button1" then) then alter the offset (c0) and mesh / texture accordingly.
Make sure the Hat Gui "Enabled" property is disabled first
7 comments
Would newweld.C0 = CFrame.new(0, 0, 0) Stay the same as 0's or would they be 1's if a new hat is added?
you are manually setting it so it would stay at (0,0,0), but you'd need to make sure the offset is correct in a test server
that's why the if-then since the mesh / texture / cframe will be different for each accessory
Ah alright.
So the 'locsl script' Is put into the TextButton that opens the gui, or the imagebutton that puts the hat on the players head?
the local script that opens the textbutton, according to this script, is placed directly into the gui that the textbutton is parented under. You should have two screen guis
Alrighty.