How do I make this script more "Player based"

Hello. I was doing some testing on my script, when this weird bug happened. When a player dies and someone else is in the server, the player loads the other persons colors back instead of their own colors back. I’m assuming this is because this script has an error that makes it not player based. What I mean by player based, is the script only gets stuff from the player and it is also server sided. I don’t know if I explained that well but yeah. Also, the script works perfectly when there is no other players in the server.

Here’s the script:

local characterCustomizationDummy = workspace:WaitForChild("CharacterCustomizationDummy")
local colorDataStore = game:GetService("DataStoreService"):GetDataStore("CharacterColorDataStore")
local accessoryDataStore = game:GetService("DataStoreService"):GetDataStore("CharacterAccessoryDataStore")

local colorCharacterEvent = characterCustomizationDummy:WaitForChild("ColorCharacterEvent")
local equipHatEvent = characterCustomizationDummy:WaitForChild("EquipHatEvent")

local playerColors
local colorData
local accessoryData

local function playerAdded(player)
	local firstCharacter = player.CharacterAdded:Wait()
	
	local playerColorsFolder = Instance.new("Folder",player)
	playerColorsFolder.Name = "PlayerColors"
	
	local equippedAccessoryValue = Instance.new("StringValue",player)
	equippedAccessoryValue.Name = "EquippedAccessory"

	for i,color in pairs(game:GetService("ReplicatedStorage"):GetChildren()) do
		if color.ClassName == "Color3Value" then
			local playerColor = color:Clone()
			playerColor.Parent = playerColorsFolder
			playerColor.Name = color.Name
			playerColor:FindFirstChild("Adornee").Value = firstCharacter:FindFirstChild(tostring(playerColor:FindFirstChild("Adornee").Value))
		end
	end
	
	local success, errormessage = pcall(function()
		colorData = colorDataStore:GetAsync("id_"..player.UserId)
		accessoryData = accessoryDataStore:GetAsync("id_"..player.UserId)
	end)
	if success then 
		playerColors = {}
		for i,color in pairs(player:FindFirstChild("PlayerColors"):GetChildren()) do
			table.insert(playerColors,color)
		end
		for i,Color in pairs(colorData) do
			for i,Value in pairs(playerColors) do
				if Value.Name == Color[1] then
					Value.Value = Color3.new(Color[2],Color[3],Color[4])
					Value:FindFirstChild("Adornee").Value.Color = Value.Value
				end
			end
		end
		
		for i,accessory in pairs(firstCharacter:GetChildren()) do
			if accessory.ClassName == "Accessory" then
				accessory:Destroy()
			end
		end

		local accessory = game:GetService("ReplicatedStorage"):WaitForChild("Accessories"):FindFirstChild(accessoryData):Clone()
		equippedAccessoryValue.Value = accessoryData
		accessory.Parent = firstCharacter
	end
	local function loadCharacterColors(character)
		local colorInfo = {}
		for i,color in pairs(playerColors) do
			table.insert(colorInfo,{color.Parent.Parent,color.Name,color.Value.R,color.Value.G,color.Value.B}) 
		end
		
		for i,color in pairs(playerColors) do
			color.Adornee.Value = character:FindFirstChild(tostring(color.Adornee.Value))
		end
		
		local success, errormessage = pcall(function()
			colorData = colorDataStore:GetAsync("id_"..player.UserId)
		end)
		for i,color in pairs(colorData) do
			for i,Value in pairs(playerColors) do
				if Value.Name == color[1] then
					Value.Value = Color3.new(color[2],color[3],color[4])
					Value:FindFirstChild("Adornee").Value.Color = Value.Value
				end
			end
		end
		
		for i,accessory in pairs(character:GetChildren()) do
			if accessory.ClassName == "Accessory" then
				accessory:Destroy()
			end
		end

		local accessory = game:GetService("ReplicatedStorage").Accessories:FindFirstChild(accessoryData):Clone()
		player:FindFirstChild("EquippedAccessory").Value = "Accessory"..accessoryData
		accessory.Parent = character
	end
	player.CharacterAdded:Connect(loadCharacterColors)
end

local function saveColorData(player)
	local colorInfo = {}
	for i,color in pairs(playerColors) do
		table.insert(colorInfo,{color.Name,color.Value.R,color.Value.G,color.Value.B}) 
	end
	local success,errormessage = pcall(function()
		colorDataStore:SetAsync("id_"..player.UserId,colorInfo)
	end)
	if success then print("saved") else error(errormessage) end
end


local function saveAccessoryData(player)
	accessoryData = player:FindFirstChild("EquippedAccessory").Value
	local success,errormessage = pcall(function()
		accessoryDataStore:SetAsync("id_"..player.UserId,accessoryData)
	end)
	if success then print("accessorySaved") else error(errormessage) end
end

local function colorPart(player,color,part)
	local character = player.Character
	character[part].Color = color
	player:FindFirstChild("PlayerColors")[part.."Color"].Value = color
	
	saveColorData(player)
end

local function equipAccessory(player,accessoryNumber)
	local character = player.Character
	for i,accessory in pairs(character:GetChildren()) do
		if accessory.ClassName == "Accessory" then
			accessory:Destroy()
		end
	end
	
	local accessory = game:GetService("ReplicatedStorage").Accessories:FindFirstChild("Accessory"..accessoryNumber):Clone()
	player:FindFirstChild("EquippedAccessory").Value = "Accessory"..accessoryNumber
	accessory.Parent = character
	saveAccessoryData(player)
end

game:GetService("Players").PlayerAdded:Connect(playerAdded)
game:GetService("Players").PlayerRemoving:Connect(saveColorData,saveAccessoryData)
colorCharacterEvent.OnServerEvent:Connect(colorPart)
equipHatEvent.OnServerEvent:Connect(equipAccessory)

Thank you for the help I appreciate it!

Also, I think it might be around the loadCharacterColors function

Can somebody please help me lol

What has printed during your debug? (If you have debugged I reccomend doing that first)

That won’t work I’m p sure. Try

game:GetService("Players").PlayerRemoving:Connect(saveAccessoryData)
game:GetService("Players").PlayerRemoving:Connect(saveColorData)

I put a print(Value.Adornee.Value.Parent) right after the Value:FindFirstChild("Adornee").Value.Color = Value.Value and it printed the correct character

That line: game:GetService("Players").PlayerRemoving:Connect(saveColorData,saveAccessoryData) actually does work

1 Like

Wait nvm I think it doesn’t print

Everyone I’m 90% sure the bug comes from the loadCharacterColors function.

Also, that bug especially happens when players die at the same time

check your shared table do a few prints around there to see what data is being stored and if it matches up with the visuals

the values are correct and everything, its just that it seems that the colors and accessories dont load on the correct character

yea so im saying print the colors that load on and the character its being loaded onto a visual would be great aswell like a mp4.

1 Like