Spawning as character not working

I’m trying to make it so the player spawns as the character of their set attribute, and when they die they also spawn as the character of their set attribute. However, there is an issue. The character is changing infinitely for no reason.

Here is a video showing an example:

And here is the code:

--//Services
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local PLAYERS = game:GetService("Players")

--//Variables
local module = require(script.Parent.MODULES.DRESS)
local trolls_folder = RS:WaitForChild("Trolls")
local health_gui = SS:WaitForChild("GUI").HEALTH_GUI

--//CharacterAdded
function character_added(character, player)
	if player:GetAttribute("Selected_Troll") == "" then
		player:SetAttribute("Selected_Troll", "Troll")
	end
	
	
	local troll = trolls_folder:FindFirstChild(player:GetAttribute("Selected_Troll"), true)

	if (troll) then
		module:switch_rig(character, troll)
	else
		warn("no troll found")
	end
	
end

--//PlayerAdded
function player_added(player)
	
	player.CharacterAdded:Connect(function(character)
		task.wait(1)
		character_added(character, player)
	end)
	
	local character = player.Character
	
	if (character) then
		character_added(character, player)
	end
end

--//Connecting
task.wait(2.5)
game.Players.PlayerAdded:Connect(player_added)

for _, player in ipairs(PLAYERS:GetPlayers()) do
	player_added(player)
end
1 Like

I also might aswell note when other players join the game their attribute character does not load.

Add debounce because it might be creating an infinite loop, also use player.CharacterAppearanceLoaded instead of CharacterAdded

Turns out CharacterAppearanceLoaded actually works. Thanks!!! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.