PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Temporary DataStore

Temporary DataStore

Sep 17 2018, 5:33 PM PST

This code sample demonstrates one possible usage of BindableFunction|BindableFunctions, temporary datastores, in combination with ClickDetector|ClickDetectors. It also demonstrates why BindableFunctions are important, since they allow multiple server Script|Scripts to communicate with each other.

Note that this can also be achieved through the use of a ModuleScript.

This example creates a datastore using a table in a Script. Since it does not utilize Roblox’s GlobalDataStore|DataStores, it is temporary and player data is erased when the player leaves or the server shuts down. For persistent DataStores, take a look at this article.

The code below contains two parts:

###TempDataStore Script
The script containing the BindableFunction/OnInvoke callback. Since this is a callback, the Invoke() function will wait for and receive the results of the invoked function. This is the TempDataStore script in the code below.

This script maintains a table called PlayerData that tracks players’ data while they are in the game. When a player enters the game, they are added to the table with a balance of 0 using their Player/UserId|UserId as the table key. When a player exits the game, their key is removed from the table. The script creates two BindableFunctions to interact with the ActivateButton script named GetData and SetData.

When GetData’s OnInvoke event fires, the script returns the player’s balance in PlayerData.

When SetData’s OnInvoke event fires, the script set’s the player’s balance in PlayerData to the value passed as an argument and returns the updated value.

###ActivateButton Script
The script containing the BindableFunction/Invoke function. This is the ActivateButton script in the code below.

It creates a ClickDetector that allows the player to retrieve and increment their balance in the temporary datastore in TempDataStore. When the player left mouse clicks on the parent part the SetData BindableFunction is Invoked, which increases the player’s balance by 1. When the player right mouse clicks on the part the GetData BindableFunction is invoked, which retrieves and prints the player’s balance.

-- TempDataStore (Script 1)
local GetData = Instance.new("BindableFunction", game.ServerStorage)
GetData.Name = "GetData"

local SetData = Instance.new("BindableFunction", game.ServerStorage)
SetData.Name = "SetData"

local PlayerData = {}

local function getData(player)
	return PlayerData[player.UserId]
end

local function setData(player, value)
	PlayerData[player.UserId] = value
	return PlayerData[player.UserId]
end

local function addPlayerData(player)
	table.insert(PlayerData, player.UserId, 0)
end

local function removePlayerData(player)
	table.remove(PlayerData, player.UserId)
end

GetData.OnInvoke = getData
SetData.OnInvoke = setData

game.Players.PlayerAdded:Connect(addPlayerData)
game.Players.PlayerRemoving:Connect(removePlayerData)




-- ActivateButton (Script 2)
local GetData = game.ServerStorage:WaitForChild("GetData")
local SetData = game.ServerStorage:WaitForChild("SetData")

local ClickDetector = Instance.new("ClickDetector", script.Parent)

local function getData(player)
	print(player.Name.."'s Current Balance: "..GetData:Invoke(player))
end

local function setData(player)
	local value = 100
	print("Set "..player.Name.."'s Balance to: "..SetData:Invoke())
end

local function incrementData(player)
	local balance = GetData:Invoke(player)
	print("Set "..player.Name.."'s Balance to: "..SetData:Invoke(player, balance+1))
end

ClickDetector.MouseClick:Connect(incrementData)
ClickDetector.RightMouseClick:Connect(getData)