CharacterAdded not working inside remote event

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)
Open

– This may be wrong, but don’t worry, there are more people to help you…

Change the scripts to the following:

--local script
local plr = game.Players.LocalPlayer
equippedItem:GetPropertyChangedSignal("Value"):Connect(function()
    if equippedItem.Value ~= "" then
            game.ReplicatedStorage.GiveTitle:FireServer(plr, 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)
1 Like

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)

1 Like

You forgot to change c to char

2 Likes

got an error from setmetatable invalid argument table expected

1 Like

Fixed, forgot to update the table name

It should be plr.Character.Head

unfortunately it still doesnt work

What is the issue? Did you put your remote event back into the script I wrote?

are you sure the character isnt already loaded? this seems kinda unknown

it still doesnt do anything same as before

Can you send your updated script? Make sure you’re running updateTitle() when the remote is fired.

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)

Your logic is mixed up. Replace it with this:


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)

now got an error from local tag = titletemplates[titles[plr]]:Clone()

Do you have a tag in your tags folder named “Default”?

I dont know what you mean by tags

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)

error attempt to index nil from local tag = titleTemplates:FindFirstChild(titles[]plr):Clone()

I just ran a quick test and this code worked for me. Try comparing your code to this file:
TitlesTest.rbxl (28.1 KB)