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

How to get his level to increase - Encountered problem ?

I am in the process of creating a script that will allow me to make a leaderstats, as I have been good at all this time, however I am completely blocked and I would like to try to understand WHY.

When the part are equal to or greater than the value 100 I make sure that it returns to zero and that it adds a value +1 to me in my level.

However I don't understand why my code is not working on my current project.

I literally copy paste my script into another blank project and the it works, but it doesn't want to work directly in my project at all, and I have no idea.

This is my code :

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("ScoreData")

local plr = game:GetService("Players")

local plrs = plr:GetPlayers()

game.Players.PlayerAdded:Connect(function(player)
	
	wait()
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "DataScore"
	leaderstats.Parent = player
	
	local part = Instance.new("IntValue")
	part.Name = "Part"
	part.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats

	local PartData 
	local LevelData

	local success, errormessage = pcall(function()
		PartData = myDataStore:GetAsync(player.UserId.."-part")
		LevelData = myDataStore:GetAsync(player.UserId.."-level")
	end)

	if success then
		part.Value = PartData
		level.Value = LevelData
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end

	while wait(300) do

		myDataStore:SetAsync(player.UserId.."-part", player.DataScore.Part.Value)
		myDataStore:SetAsync(player.UserId.."-level", player.DataScore.Level.Value)
		print("Data is Saved")

	end
	
	
	local checklevel = coroutine.create(function()
		while wait(1) do
			if (player.DataScore.Part.Value == 100 or player.DataScore.Part.Value >= 100) then
				player.DataScore.Part.Value = 0
				player.DataScore.Level.Value = player.DataScore.Level.Value + 1
				print("Level Up Success")
				myDataStore:SetAsync(player.UserId.."-part", player.DataScore.Part.Value)
				myDataStore:SetAsync(player.UserId.."-level", player.DataScore.Level.Value)
				print("Data is Saved")
			end
		end
	end)
	
	coroutine.resume(checklevel)
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-part", player.DataScore.Part.Value)
		myDataStore:SetAsync(player.UserId.."-level", player.DataScore.Level.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

Where I have a problem :

	local checklevel = coroutine.create(function()
		while wait(1) do
			if (player.DataScore.Part.Value == 100 or player.DataScore.Part.Value >= 100) then
				player.DataScore.Part.Value = 0
				player.DataScore.Level.Value = player.DataScore.Level.Value + 1
				print("Level Up Success")
				myDataStore:SetAsync(player.UserId.."-part", player.DataScore.Part.Value)
				myDataStore:SetAsync(player.UserId.."-level", player.DataScore.Level.Value)
				print("Data is Saved")
			end
		end
	end)
	
	coroutine.resume(checklevel)

As I can see on my project it doesn't work, however when I copy paste the script like I said earlier in my posts, on another blank project the code works.

I don't understand why, yet the options for my project are good.

Other code :

	local leveling = coroutine.create(function()
		while wait(60) do
			level.Value = level.Value + 1
			character.Head.NameTag.Level.Text = "Level " .. level.Value
			DS:SetAsync(player.userId, {level.Value})
		end
	end)
	
	coroutine.resume(leveling)

For example on one of my other project I did this, and it worked, however I don't understand why on my project with another version it does not work.

1 answer

Find !

It was because of the while wait (300) do which was above my coroutine.

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