Working on a shop gui and ran into a problem where the characteradded event doesn’t appear to work in a remote event. What i want the script to do is parent the item that has been equipped into the player’s model and if the player respawns the item should stay inside of the player’s model. Since characteradded doesnt work, how would i achieve what i want the script to do? Any other ways instead characteradded?
--local script
equippedItem:GetPropertyChangedSignal("Value"):Connect(function()
if equippedItem.Value ~= "" then
game.ReplicatedStorage.GiveTitle:FireServer(equippedItem.Value)
end
end)
-- script
game.ReplicatedStorage.GiveTitle.OnServerEvent:Connect(function(plr,itemName)
local cloneditem = game.ServerStorage.titles[itemName]:Clone()
plr.CharacterAdded:Connect(function(char)
cloneditem.Parent = char.Head
end)
end)
The clone is being destroyed when the character is removed so it’s nil every time the character is added. I’d suggest having a table of tags and automatically checking even before a player fires an event.
local titles = {}
local titleTemplates = game:GetService("ServerStorage").titles
local function updateTitle(plr, titleName)
titles[plr] = titleName
local prev = plr.Character.Head:FindFirstChildOfClass("BillboardGui") -- assuming your tags are BillboardGuis
if prev then prev:Destroy() end
local tag = titleTemplates[titleName]:Clone()
local head = plr.Character.Head
tag.Parent = head
tag.Adornee = head
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
titles[plr] = "Default"
plr.CharacterAdded:Connect(function(c)
local tag = titleTemplates:FindFirstChild(titles[plr]):Clone()
local head = c:WaitForChild("Head")
tag.Parent = head
tag.Adornee = head
end)
end)
game.ReplicatedStorage.GiveTitle.OnServerEvent:Connect(function(plr,itemName)
local titles = {}
setmetatable(titles, {__index = "Default"})
local titleTemplates = game:GetService("ServerStorage").titles
local function updateTitle(plr, titleName)
titles[plr] = titleName
local prev = plr.Character.Head:FindFirstChildOfClass("BillboardGui") -- assuming your tags are BillboardGuis
if prev then prev:Destroy() end
local tag = titleTemplates[titleName]:Clone()
local head = plr.Character.Head
tag.Parent = head
tag.Adornee = head
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(c)
local tag = titleTemplates[titles[plr]]:Clone()
local head = c:WaitForChild("Head")
tag.Parent = head
tag.Adornee = head
end)
end)
end)
local titles = {}
local titleTemplates = game:GetService("ServerStorage").titles
local function updateTitle(plr, titleName)
titles[plr] = titleName
local prev = plr.Character.Head:FindFirstChildOfClass("BillboardGui") -- assuming your tags are BillboardGuis
if prev then prev:Destroy() end
local tag = titleTemplates[titleName]:Clone()
local head = plr.Character.Head
tag.Parent = head
tag.Adornee = head
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
titles[plr] = "Default"
plr.CharacterAdded:Connect(function(c)
local tag = titleTemplates:FindFirstChild(titles[plr]):Clone()
local head = c:WaitForChild("Head")
tag.Parent = head
tag.Adornee = head
end)
end)
game:GetService("ReplicatedStorage").GiveTitle.OnServerEvent:Connect(function(plr,itemName)
updateTitle(plr, itemName)
end)
I mean titles, sorry. Try this instead of metatables:
local titles = {}
local titleTemplates = game:GetService("ServerStorage").titles
local function updateTitle(plr, titleName)
titles[plr] = titleName
local prev = plr.Character.Head:FindFirstChildOfClass("BillboardGui") -- assuming your tags are BillboardGuis
if prev then prev:Destroy() end
local tag = titleTemplates[titleName]:Clone()
local head = plr.Character.Head
tag.Parent = head
tag.Adornee = head
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
titles[plr] = "Default"
plr.CharacterAdded:Connect(function(c)
local tag = titleTemplates:FindFirstChild(titles[plr]):Clone()
local head = c:WaitForChild("Head")
tag.Parent = head
tag.Adornee = head
end)
end)
game:GetService("ReplicatedStorage").GiveTitle.OnServerEvent:Connect(function(plr,itemName)
updateTitle(plr, itemName)
end)