Code doesn't run first time

This is the code I use for data in my game, when the player first joins the game some things dont run(for example, character added function. when the player resets, it works fine. I debugged it and still don’t know why it happens(printing numbers is debugging in my code). The first time player joins it prints out 4 and 2, after they reset it prints out all numbers.

local work = game:GetService("Workspace")
local rs = game:GetService("ReplicatedStorage")
local data_service = game:GetService("DataStoreService")
local total_kills_data = data_service:GetOrderedDataStore("Total kills")
local streak_data = data_service:GetOrderedDataStore("Streak")
local players = game:GetService("Players")
local events = rs.Events
local reach_event = events:WaitForChild("ReachMessanger", 10)

players.PlayerAdded:Connect(function(player)
	local playerKey = "Player_" .. player.UserId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	local totalkills = Instance.new("IntValue")
	totalkills.Name = "Total kills"
	totalkills.Parent = leaderstats
	local streak = Instance.new("IntValue")
	streak.Name = "Streak"
	streak.Parent = leaderstats
	local total_streak = Instance.new("IntValue")
	total_streak.Name = "Total streak"
	total_streak.Parent = player
	print(1)
	local myStreak

	local success, err = pcall(function()
		myStreak = streak_data:GetAsync(playerKey)
	end)

	if success then
		total_streak.Value = myStreak
	else
		total_streak.Value = 0
	end
	total_streak.Value = streak_data:GetAsync(player.UserId) or 0
	streak_data:SetAsync(player.UserId, totalkills.Value)
	total_streak.Changed:Connect(function()
		streak_data:SetAsync(player.UserId, totalkills.Value)	
	end)
	print(4)
	
	local myKills

	local success, err = pcall(function()
		myKills = total_kills_data:GetAsync(playerKey)
	end)

	if success then
		totalkills.Value = myKills
	else
		totalkills.Value = 0
	end
	totalkills.Value = total_kills_data:GetAsync(player.UserId) or 0
	total_kills_data:SetAsync(player.UserId, totalkills.Value)
	totalkills.Changed:Connect(function()
		total_kills_data:SetAsync(player.UserId, totalkills.Value)	
	end)
	print(2)
	player.CharacterAdded:Connect(function(char)
		print(5)
		task.wait()
		print(3)
		local Humanoid = char:FindFirstChild("Humanoid")
		local root = char:WaitForChild("HumanoidRootPart", 10)
		local pos = Vector3.new(math.random(0,125), 30, math.random(0,125))
		char:MoveTo(pos)
		print("Spawned "..char.Name.." at "..tostring(pos))
		if not Humanoid:FindFirstChild("Killer") then
			local new_tag = Instance.new("ObjectValue")
			new_tag.Name = "Killer"
			new_tag.Parent = Humanoid
		end
		Humanoid.Died:Connect(function(died)
			deaths.Value += 1
			if streak_data:GetAsync(player.UserId) < streak.Value then
				total_streak.Value = streak
			end
			streak.Value = 0
			local tag = Humanoid:FindFirstChild("Killer")
			local killer = tag.Value
			local killer_humanoid
			local killer_char
			if killer.Character then
				killer_humanoid = killer.Character:WaitForChild("Humanoid", 5)
				killer_char = killer.Character
			end
			if tag and killer and killer ~= nil and killer ~= player and killer_humanoid then
				killer.leaderstats:FindFirstChild("Kills").Value += 1
				killer.leaderstats:FindFirstChild("Total kills").Value += 1
				killer.leaderstats:FindFirstChild("Streak").Value += 1
				killer_humanoid.Health = killer_humanoid.MaxHealth
			end
		end)
	end)
end)

This happens because your code runs after their character loads(due to API calls it makes), although this can easily be fixed by making an exclusion for the first time(assuming their character may already be there):

local function CharacterAdded(char)
	--code 
end

CharacterAdded(player.Character or player.CharacterAdded:Wait()) --first time
player.CharacterAdded:Connect(CharacterAdded) --rest of the times
1 Like

CharacterAdded isn’t connected in time for the Character getting added, instead do:

local work = game:GetService("Workspace")
local rs = game:GetService("ReplicatedStorage")
local data_service = game:GetService("DataStoreService")
local total_kills_data = data_service:GetOrderedDataStore("Total kills")
local streak_data = data_service:GetOrderedDataStore("Streak")
local players = game:GetService("Players")
local events = rs.Events
local reach_event = events:WaitForChild("ReachMessanger", 10)

players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
		print(5)
		task.wait()
		print(3)
		local Humanoid = char:FindFirstChild("Humanoid")
		local root = char:WaitForChild("HumanoidRootPart", 10)
		local pos = Vector3.new(math.random(0,125), 30, math.random(0,125))
		char:MoveTo(pos)
		print("Spawned "..char.Name.." at "..tostring(pos))
		if not Humanoid:FindFirstChild("Killer") then
			local new_tag = Instance.new("ObjectValue")
			new_tag.Name = "Killer"
			new_tag.Parent = Humanoid
		end
		Humanoid.Died:Connect(function(died)
			deaths.Value += 1
			if streak_data:GetAsync(player.UserId) < streak.Value then
				total_streak.Value = streak
			end
			streak.Value = 0
			local tag = Humanoid:FindFirstChild("Killer")
			local killer = tag.Value
			local killer_humanoid
			local killer_char
			if killer.Character then
				killer_humanoid = killer.Character:WaitForChild("Humanoid", 5)
				killer_char = killer.Character
			end
			if tag and killer and killer ~= nil and killer ~= player and killer_humanoid then
				killer.leaderstats:FindFirstChild("Kills").Value += 1
				killer.leaderstats:FindFirstChild("Total kills").Value += 1
				killer.leaderstats:FindFirstChild("Streak").Value += 1
				killer_humanoid.Health = killer_humanoid.MaxHealth
			end
		end)
end)


	local playerKey = "Player_" .. player.UserId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	local totalkills = Instance.new("IntValue")
	totalkills.Name = "Total kills"
	totalkills.Parent = leaderstats
	local streak = Instance.new("IntValue")
	streak.Name = "Streak"
	streak.Parent = leaderstats
	local total_streak = Instance.new("IntValue")
	total_streak.Name = "Total streak"
	total_streak.Parent = player
	print(1)
	local myStreak

	local success, err = pcall(function()
		myStreak = streak_data:GetAsync(playerKey)
	end)

	if success then
		total_streak.Value = myStreak
	else
		total_streak.Value = 0
	end
	total_streak.Value = streak_data:GetAsync(player.UserId) or 0
	streak_data:SetAsync(player.UserId, totalkills.Value)
	total_streak.Changed:Connect(function()
		streak_data:SetAsync(player.UserId, totalkills.Value)	
	end)
	print(4)
	
	local myKills

	local success, err = pcall(function()
		myKills = total_kills_data:GetAsync(playerKey)
	end)

	if success then
		totalkills.Value = myKills
	else
		totalkills.Value = 0
	end
	totalkills.Value = total_kills_data:GetAsync(player.UserId) or 0
	total_kills_data:SetAsync(player.UserId, totalkills.Value)
	totalkills.Changed:Connect(function()
		total_kills_data:SetAsync(player.UserId, totalkills.Value)	
	end)
	print(2)
end)
1 Like