Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to use data stores via local script through remote events/functions?

Asked by
Soban06 410 Moderation Voter
3 years ago
Edited 3 years ago

So I have a skip stage system in my game. I have a GUI. Inside that GUI, I have a local script.

There is a limits variable which checks how many skip stages do you have left. Default is 3. So whenever a player uses a skip stage, the game checks if he has at least 1 skip stage left and if he does, the game reduces the limits variable by 1. So each time the player uses a skip stage, the limits variable will be reduced.

So the problem was that when a player leaves the game, the limits variable resets to 3. Which I don't want. I want the limits variable to stay at the amount at which it was before the player left the game. So I tried using data stores to see if it will work. I am not sure if it will work or not.

But the output says that you cannot access data stores from client i.e. local scripts.

So I thought of using remote events to do the trick. I know how to use remote events but not with data stores. I am a beginner/intermediate in scripting so please help.

Here is what I tried:

My Local Script inside the Skip Stage GUI:

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)

P.S: Do you think I am implementing the data stores correctly?

And how do I make the above code with remote events so that I can use data stores via local scripts?

Thanks

0
Maybe add a ModuleScript? This way you can access everything from the script Lightning_Game27 232 — 3y
0
agreed CaIcuIati0n 246 — 3y
1
Module Scripts mantain the current scope, so if you require it locally, it will be considered a LocalScript, if you require it from a ServerScript it will be considered a ServerScript Leamir 3138 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

To help prevent exploits, you can set it up like this:

Server:

  • Uses data stores to load the number of skips a player has
  • Keeps track of number of skips for all players in the server (either using a table or an IntValue. An IntValue stored as a child or descendant of the player allows the client script to easily read the number of skips they have left.)
  • Responds to a RemoteEvent where the player is asking for a skip by performing the teleportation
  • If you wanted to prevent exploiting, you'd also want to make sure that players aren't teleporting/flying unless you perform the teleport (if you attempt this, try not to get any false positives - ex, due to network latency someone might go substantially faster than you'd expect. If someone resets or falls of the map, naturally you don't want to assume they're exploiting because of that)

Client:

  • When the player activates the button to skip, use that RemoteEvent to request the server to skip the stage

There is a tutorial for how to use data stores here: https://developer.roblox.com/en-us/articles/Saving-Player-Data

Ad

Answer this question