Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED] Character data gets replaced with another player's char data?

Let's say I've made a character in the customization part. I teleport to the main game. My character is there. But once another player joins, my character gets replaced with his character.

-- local script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local TeleportService = game:GetService("TeleportService")
local playerData = TeleportService:GetLocalPlayerTeleportData()

print(playerData)
game.ReplicatedStorage.Remotes.GetData:FireServer(playerData)
-- server script
local module = require(script.Parent.DataModule) -- the module script doesn't have anything that influences my problem
local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local key = module.generateKey(player)
			
	local data = Instance.new("Folder")
	data.Name = "PlayerData"
	data.Parent = player
	
	local magicType = Instance.new("StringValue")
	magicType.Name = "MagicType"
	magicType.Parent = data
	
	local magicLevel = Instance.new("IntValue")
	magicLevel.Name = "MagicLevel"
	magicLevel.Parent = magicType
	
	local magicXP = Instance.new("IntValue")
	magicXP.Name = "MagicXP"
	magicXP.Parent = data
	
	local magicXPN = Instance.new("IntValue")
	magicXPN.Name = "MagicXPNeeded"
	magicXPN.Parent = data
	magicXPN.Value = 20
	
	local magicEnergy = Instance.new("IntValue")
	magicEnergy.Name = "MagicEnergy"
	magicEnergy.Parent = data
	magicEnergy.Value = 30
	
	local levels = Instance.new("IntValue")
	levels.Name = "Levels"
	levels.Parent = data
	levels.Value = 1
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Parent = levels
	
	local xpn = Instance.new("IntValue")
	xpn.Name = "XPNeeded"
	xpn.Parent = levels
	xpn.Value = 100
	
	local silver = Instance.new("IntValue")
	silver.Name = "Silver"
	silver.Parent = data
	
	local hp = Instance.new("IntValue")
	hp.Parent = data
	hp.Name = "HP"
	hp.Value = char:WaitForChild("Humanoid").MaxHealth
	
	local hair1 = Instance.new("StringValue")
	hair1.Name = "Hair1"
	hair1.Parent = data
	
	local hair1Color = Instance.new("Vector3Value")
	hair1Color.Name = "Hair1Color"
	hair1Color.Parent = data
	
	local hair1Offset = Instance.new("Vector3Value")
	hair1Offset.Name = "Hair1Offset"
	hair1Offset.Parent = data	
	
	local hair2 = Instance.new("StringValue")
	hair2.Name = "Hair2"
	hair2.Parent = data
	
	local hair2Color = Instance.new("Vector3Value")
	hair2Color.Name = "Hair2Color"
	hair2Color.Parent = data
	
	local hair2Offset = Instance.new("Vector3Value")
	hair2Offset.Name = "Hair2Offset"
	hair2Offset.Parent = data	
	
	local skinColor = Instance.new("Color3Value")
	skinColor.Name = "SkinColor"
	skinColor.Parent = data
	
	local face = Instance.new("StringValue")
	face.Name = "Face"
	face.Parent = data
	
	local shirt = Instance.new("StringValue")
	shirt.Name = "Shirt"
	shirt.Parent = data
	
	local shirtColor = Instance.new("Color3Value")
	shirtColor.Name = "ShirtColor"
	shirtColor.Parent = data
	
	local pants = Instance.new("StringValue")
	pants.Name = "Pants"
	pants.Parent = data
	
	local pantsColor = Instance.new("Color3Value")
	pantsColor.Name = "PantsColor"
	pantsColor.Parent = data
	
	local dataa = module.generateData(player)
	
	game.ReplicatedStorage.Remotes.GetData.OnServerEvent:Connect(function(player, decodedData)
		spawn(function()
			wait(1)
			for i, v in pairs(char:GetChildren()) do
				if v:IsA("Accessory") then
					v:Destroy()
				end
			end
			
			for i, v in pairs(game.ServerStorage:GetChildren()) do
				if v:IsA("Accessory") then
					v:Clone().Parent = char
				end
			end
		end)		
		
		print(decodedData)	
		
		magicEnergy.Value = decodedData.magicEnergy
		xp.Value = decodedData.xp
		xpn.Value = decodedData.xpn
		levels.Value = decodedData.levels
		silver.Value = decodedData.silver
		hp.Value = decodedData.hp
		magicLevel.Value = decodedData.magicLevel
		magicXP.Value = decodedData.magicXP
		magicXPN.Value = decodedData.magicXPN
		magicType.Value = decodedData.magicType
		
		hair1.Value = decodedData.hair1
		hair1Color.Value = decodedData.hair1Color
		hair1Offset.Value = decodedData.hair1Offset
		hair2.Value = decodedData.hair2
		hair2Color.Value = decodedData.hair2Color
		hair2Offset.Value = decodedData.hair2Offset
		skinColor.Value = decodedData.skinColor
		face.Value = decodedData.face
		shirt.Value = decodedData.shirt
		shirtColor.Value = decodedData.shirtColor
		pants.Value = decodedData.pants
		pantsColor.Value = decodedData.pantsColor
		
		char:WaitForChild("Hair").Handle.Mesh.MeshId = hair1.Value
		char:WaitForChild("Hair").Handle.Mesh.VertexColor = hair1Color.Value
		char:WaitForChild("Hair").Handle.Mesh.Offset = hair1Offset.Value
		char:WaitForChild("Hair2").Handle.Mesh.MeshId = hair2.Value
		char:WaitForChild("Hair2").Handle.Mesh.VertexColor = hair2Color.Value
		char:WaitForChild("Hair2").Handle.Mesh.Offset = hair2Offset.Value
		char:WaitForChild("Body Colors").HeadColor3 = skinColor.Value
		char:WaitForChild("Body Colors").LeftArmColor3 = skinColor.Value
		char:WaitForChild("Body Colors").RightArmColor3 = skinColor.Value
		char:WaitForChild("Body Colors").LeftLegColor3 = skinColor.Value
		char:WaitForChild("Body Colors").RightLegColor3 = skinColor.Value
		char:WaitForChild("Body Colors").TorsoColor3 = skinColor.Value
		char.Head.face.Texture = face.Value
		char:WaitForChild("Shirt").ShirtTemplate = shirt.Value
		char:WaitForChild("Shirt").Color3 = shirtColor.Value
		char:WaitForChild("Pants").PantsTemplate = pants.Value
		char:WaitForChild("Pants").Color3 = pantsColor.Value

		player:WaitForChild("PlayerGui").LevelName.Level.Text = "Level "..levels.Value	
		
		if levels.Value == 1 then
		    print("Start tutorial")
		    game.ReplicatedStorage.Remotes.StartTutorial:FireClient(player)
		end		
		
		for i,v in pairs(data:GetChildren()) do
			print(tostring(v)..": ".. tostring(v.Value))
		end	
	end)
end)

game.Players.PlayerRemoving:Connect(module.saveData)

This is how it looks like in-game: image

1 answer

I've found the solution. I had to remove the OnServerEvent part and put it outside the PlayerAdded event.

This discussion is preserved as it was. New answers, comments, and votes are closed.