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

How to save limits value in Skip Stage System?

So I have a Skip Stage Feature in my game. Inside my Skip Stage Script, I first check if the player has enough skips left (which are 3 by default). Whenever the player clicks on the skip stage GUI, if the player has at least 1 skip stage left, we teleport them to the next stage. And 1 is reduced from the limits variable. So now the player has none limits left so they cannot teleport to the next level. But the problem is, if they rejoin the game, they again have 3 skip stage and not 0 or the amount at which they had when they left the game.

So whenever they leave the game, and rejoin, they again have 3 skip stages.

I can't figure out how to save their amount of skip stages left when they leave the game.

Here is my Local Script inside the Skip Stage GUI:

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats"):FindFirstChild("Checkpoint")
local hasLimits = true
local Limits = 3 -- This Limits is the amount of skip stages a player has. It resets to 3 whenever they join the game. Which I don't want.
local maxLevels = game.Workspace.MaxStages.Value -- This is the total amount of stages in the game.

script.Parent.MouseButton1Click:connect(function()
	if hasLimits == true then
		if Limits ~= 0 then
			if leaderstats.Value < maxLevels then
				Limits = Limits - 1
				leaderstats.Value = leaderstats.Value + 1
				if player ~= nil then
					if player.Character ~= nil then
						if player.Character:FindFirstChild("Humanoid").Health > 0 then
						
						player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0)

					end
				end
			end
		end
	else
		if player ~= nil then
			if player.Character ~= nil then
				if player.Character:FindFirstChild("Humanoid").Health > 0 then
					player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0)
					end
				end
			end
		end
	end
end)

Thanks

1 comment

0

Maybe add a ModuleScript in ServerStorage for the Limits and then save data using my method?

2 answers

Use datastores

2 comments

0

But I don't know how to use data stores to store variables values inside scripts. I know normal data stores like saving the leader board. But not variables inside scripts.

0

This should be a comment due to how short it is

Use the Roblox DataStoreService!

I am not entirely sure if the below works as I am a beginner scripter! If there are any errors with the script below, please tell me with the output!

Anyway, here is how to do it:

First, you have to Enable Studio access to API services. It is in Game Settings -> Options

Then at the top of your code write:

local dataStoreService = game:GetService("DataStoreService") -- Calling DataStoreService
local limitsData = dataStoreService:GetDataStore("NameThisWhateverYouWant") -- Making a new datastore called NameThisWhateverYouWant

Go to the bottom of your code and write:

game.Players.PlayerAdded:Connect(function() -- When a player is added
Limits = limitsData:GetAsync("AnyKeyYouWant") -- Getting the data that is saved.
print("Limits left:", Limits) -- Printing the data that is saved!

end)

game.Players.PlayerRemoving:Connect(function() -- When a player exits the game

print("Saving number of limits:", Limits) -- Printing the limits that are going to be saved
limitsData:SetAsync("AnyKeyYouWant", Limits) -- Saving the limits to a key. The key is used to retrieve data when called.

end)

In the end your code is going to look like this:

local dataStoreService = game:GetService("DataStoreService") -- Calling DataStoreService
local limitsData = dataStoreService:GetDataStore("NameThisWhateverYouWant") -- Making a new datastore called NameThisWhateverYouWant

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats"):FindFirstChild("Checkpoint")
local hasLimits = true
local Limits = 3 -- This Limits is the amount of skip stages a player has. It resets to 3 whenever they join the game. Which I don't want.
local maxLevels = game.Workspace.MaxStages.Value -- This is the total amount of stages in the game.

game.Players.PlayerAdded:Connect(function() -- When a player is added
Limits = limitsData:GetAsync("AnyKeyYouWant") -- Getting the data that is saved.
print("Limits left:", Limits) -- Printing the data that is saved!

end)

script.Parent.MouseButton1Click:connect(function()
    if hasLimits == true then
        if Limits ~= 0 then
            if leaderstats.Value < maxLevels then
                Limits = Limits - 1
                leaderstats.Value = leaderstats.Value + 1
                if player ~= nil then
                    if player.Character ~= nil then
                        if player.Character:FindFirstChild("Humanoid").Health > 0 then

                        player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0)

                    end
                end
            end
        end
    else
        if player ~= nil then
            if player.Character ~= nil then
                if player.Character:FindFirstChild("Humanoid").Health > 0 then
                    player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0)
                    end
                end
            end
        end
    end
end)



game.Players.PlayerRemoving:Connect(function() -- When a player exits the game

print("Saving number of limits:", Limits) -- Printing the limits that are going to be saved
limitsData:SetAsync("AnyKeyYouWant", Limits) -- Saving the limits to a key. The key is used to retrieve data when called.

end)

5 comments

0

The output is saying "DataStore can't be accessed from client".

0

because it needs to be in a serverscript facepalm

0

But the above code needs to be in a local script. Not a script. Otherwise, it will not work.

0

Oh...

0

I have an idea!

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