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

how do i make a value permanent through servers, as in unlocking a power or something?

Asked by 4 years ago

so im trying to make a shop and i need an item to permanently set a value to unlock a power, how exactly would i do this? here's the item's code so far:

Tool = script.Parent
local replicatedstorage = game:GetService("ReplicatedStorage")
local useevent = replicatedstorage.BuyDistraction
local cd = false

Tool.Activated:Connect(function()
    if cd == true then return end
    cd = true
    local plr = game.Players.LocalPlayer
    local char = plr.Character
    local hum = char.Humanoid
    useevent:FireServer()
end)

and here's the useevent

game.ReplicatedStorage.BuyDistraction.OnServerEvent:Connect(function(player)

end)

it is empty right now, as i do not know what to put in it. any help would be appreciated!

0
The only way to make stuff like that persistent through servers is to use data storing. Data storing saves information onto roblox's servers, and will then apply them to the player when entering the game again. It's a bit difficult to learn, but the dev forum explains a lot of it. https://developer.roblox.com/en-us/articles/Data-store WizyTheNinja 834 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

You may achieve data persistence via the use of DataStores. These are essentially a direct way to save data onto Roblox's servers, but you should be mindful of their limits. An implementation of them into your code would look like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService") -- https://developer.roblox.com/en-us/api-reference/class/DataStoreService

local dataStore = DataStoreService:GetDataStore("DataStore_Name") -- Retrieves a DataStore with the given name.

ReplicatedStorage.BuyDistraction.OnServerEvent:Connect(function(Player)
    pcall(function() -- DataStores may often fail to save information if something goes wrong; pcall allows your script to keep executing if what's wrapped inside of pcall's function() throws an error. This is especially used in error handling: it returns two values, one being a boolean indicating the function's success, and the other being the error message that would've otherwise been printed to the output window.
        dataStore:SetAsync(Player.UserId, true) -- Sets the value of the DataStore key that is equal to the player's User ID to true (assuming all you need is to check whether they own the power via a boolean).
    end)
end)

This essentially saves the boolean true to the key [UserId] within the DataStore upon receiving OnServerEvent from the remote. To handle DataStore errors and actually retrieve data on join is another story. On a side note: sanity checks. You definitely need to add some way for the server to check the validity of what's being sent by the Client via BuyDistraction, or else anyone with a low level injector could exploit it.

Ad

Answer this question