Why is this simple script not loading?

Hi Guys, so I made a simple script of obby for someone and it is basically for checkpoints and badges and stage system. But I noticed something, like when I join, the stage’s default value is setted to 0 by the script, so I don’t know why but it thinks that my stage is 0 and it teleports me to 0 stage but suddenly in nanoseconds it changes my stage to my actual stage but as the script thought my stage is 0 before so I get at 0 stage and I need to reset to get to my actual stage. Here’s the script -

local BS = game:GetService("BadgeService")
local function awardBadge(player, id)
	if not BS:UserHasBadgeAsync(player.UserId, id) then
		BS:AwardBadge(player.UserId, id)
	end
end
local checkpoints = workspace.Checkpoints -- It is the folder where all the checkpoints models are saved. For example, there are models with their stage no. like 1,2,3 etc. and they contain a part with common name "Checkpoint" and we move it their by the code below.

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Stage = Instance.new("IntValue", leaderstats)
	Stage.Name = "Stage"
	Stage.Value = 0
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[Stage.Value].Checkpoint.Position)
		hum.Touched:Connect(function(hit)
			if hit.Name == "Checkpoint" and hit.Parent.Parent == checkpoints then
				if tonumber(hit.Parent.Name) == Stage.Value + 1 then
					Stage.Value += 1
				end
			end
		end)
	end)
	awardBadge(player, 2130439793)
end)

So I want to know that what should I change, and why the script is so slow to update that my stage is not 0 ? Is it because of my Data Saving Script is slow (it’s a different script though) ? Thanks :slight_smile:

As it is an issue with saving/loading, we will need to see the saving script as well.

character not loaded once again

local BS = game:GetService("BadgeService")

local function awardBadge(player, id)
	if not BS:UserHasBadgeAsync(player.UserId, id) then
		BS:AwardBadge(player.UserId, id)
	end
end

local checkpoints = workspace.Checkpoints -- It is the folder where all the checkpoints models are saved. For example, there are models with their stage no. like 1,2,3 etc. and they contain a part with common name "Checkpoint" and we move it their by the code below.

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Stage = Instance.new("IntValue", leaderstats)
	Stage.Name = "Stage"
	Stage.Value = 0
	
	local function characterAdded(char)
		local hum = char:WaitForChild("Humanoid", 1 / 0)
		char:MoveTo(checkpoints[Stage.Value].Checkpoint.Position)
		
		hum.Touched:Connect(function(hit)
			if hit.Name == "Checkpoint" and hit.Parent.Parent == checkpoints and tonumber(hit.Parent.Name) == Stage.Value + 1 then
				Stage.Value += 1
			end
		end)
	end
	
	characterAdded(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(characterAdded)
	
	awardBadge(player, 2130439793)
end)

also you didn’t even save the data so you are asking for a saver script?

Can you show the save script? I think the problem is you setting stage value to 0

@SeargentAUS @BirdieI90 and @AlienX1258 , here’s the Data Saving Script too -

local DS = game:GetService("DataStoreService"):GetDataStore("Stage")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local success, err = pcall(function()
		data = DS:GetAsync(player.UserId)
	end)
	if success then
		player.leaderstats.Stage.Value = data
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data
	local success, err = pcall(function()
		data = DS:SetAsync(player.UserId, player.leaderstats.Stage.Value)
	end)
	if success then
		print("Data Saved")
	else
		warn(err)
	end
end)

game:BindToClose(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		v:Kick("Server Closed")
	end
end)

Datastores take some time to load; it’s normal that this is happening.

1 Like

Ok, but in my other obbies, this didn’t took much time and it creates a problem that it is taking so much time as it ruins the whole system. But right now I indented the code again and it got fixed I don’t know how.

1 Like

i recommend you use profile service since its faster

https://madstudioroblox.github.io/ProfileService/

1 Like