it does the same thing as before, no prints
the only thing different is it prints “saved DATA” after i leave
Then it is something with the OnUpdate, I have to ask, why is
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
print("Cool")
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)
Inside of the OnUpdate for the InventoryStore, shouldn’t it be outside of it? Because the only way it will update the CurrnecyStore is if the InventoryStore was updated once, which from your showcase, never happens
If you’re firing a remote event from the server to the client you need to pass a player parameter. It looks like you haven’t here.
how do i add it outside?
InventoryStore:OnUpdate(function(decodedData)
inventoryString.Value = HttpService:JSONEncode(decodedData)
end)
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
print("Cool")
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)
print("Again")
Basically, just cut the code from the OnUpdate besides the changing of the inentoryString and place it outside
like this?
InventoryStore:OnUpdate(function(decodedData)
inventoryString.Value = HttpService:JSONEncode(decodedData)
end)
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
print("Cool")
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)
print("Again")
end)
That is correct, try it out now
YES, IT WORKS tysm
Anytime! If you have any other issues don’t be afraid to make another post!
How would i do the same thing for level?
local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
DataStore2.Combine("Data", "Inventory", "Currency", "Level", "Exp")
local defaultLevel = 1
local defaultexp = 0
local DefaultCurrencyAmount = 0
local DefaultHealthAmount = 150
local expToLeveUp = function(level)
return 100 + level * 5
end
game.Players.PlayerAdded:Connect(function(player)
--All the DataStores in here--
local InventoryStore = DataStore2("Inventory", player)
local CurrencyStore = DataStore2("Currency", player)
local ExpStore = DataStore2("Exp", player)
local LevelStore = DataStore2("Level", player)
local replicatedDataFolder = Instance.new("Folder")
replicatedDataFolder.Parent = replicatedStorage.ReplicatedData
replicatedDataFolder.Name = player.UserId
---------------------------------------------
--STARTING THE INVENTORY FUNCTION--
local inventoryString = Instance.new("StringValue")
inventoryString.Parent = replicatedDataFolder
inventoryString.Name = "Inventory"
--Leaderstats--
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local LevelValue = Instance.new("IntValue", leaderstats)
LevelValue.Name = "Level"
local expValue = Instance.new("IntValue", leaderstats)
expValue.Name = "Exp"
----------------------------------------------
--Functions--
local function updateLevel(level)
player.leaderstats.Level.Value = level
end
local function updateEXP(exp)
if exp >= expToLeveUp(LevelStore:Get(defaultLevel)) then
ExpStore:Increment(expToLeveUp(LevelStore:Get(defaultLevel)) * -1)
LevelStore:Increment(1)
else
player.leaderstats.Exp.Value = exp
end
end
local inventoryData = InventoryStore:Get({})
inventoryString.Value = HttpService:JSONEncode(inventoryData)
InventoryStore:OnUpdate(function(decodedData)
inventoryString.Value = HttpService:JSONEncode(decodedData)
end)
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)
--call functions right away one time
updateLevel(LevelStore:Get(defaultexp))
updateEXP(ExpStore:Get(defaultexp))
--call function right away one time
local function updateClientLevel(amount)
replicatedStorage.Events.UpdateClientLevel:FireClient(player, amount)
end
LevelStore:OnUpdate(updateLevel)
ExpStore:OnUpdate(updateEXP)
end)
game.Workspace:WaitForChild("Bruh").ClickDetector.MouseClick:Connect(function(player)
local CurrencyStore = DataStore2("Currency", player)
CurrencyStore:Increment(1,DefaultCurrencyAmount)
end)
That looks like it should work, in the ClickDetector code, turn it to this
game.Workspace:WaitForChild("Bruh").ClickDetector.MouseClick:Connect(function(player)
local CurrencyStore = DataStore2("Currency", player)
local ExpStore = DataStore2("Exp", player)
CurrencyStore:Increment(1,DefaultCurrencyAmount)
ExpStore:Increment(10,defaultexp) --10 is an example number, you can use some other number if needed
end)
in a different script i have it every 3 seconds it increments by 5, how do i make the level show for the text label? and the exp
Just do what you did for the Currency but change it for the textlabels for Level and exp, I also recommend putting all of this in a single script to not make unneeded scripts, such as putting the code in the same script you put the currency event code in
I did what i did for the currency one but it doesn’t work what did i do wrong?
Show me the code for both the event code and the leaderstats script
This one is the local script inside the text label for the level
game.Players.PlayerAdded:Connect(function(player)
script.Parent.Text = "Level:".. player.leaderstats.Levels
end)
Data Handler
local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
DataStore2.Combine("Data", "Inventory", "Currency", "Level", "Exp")
local defaultLevel = 1
local defaultexp = 0
local DefaultCurrencyAmount = 0
local DefaultHealthAmount = 150
local expToLeveUp = function(level)
return 100 + level * 5
end
game.Players.PlayerAdded:Connect(function(player)
--All the DataStores in here--
local InventoryStore = DataStore2("Inventory", player)
local CurrencyStore = DataStore2("Currency", player)
local ExpStore = DataStore2("Exp", player)
local LevelStore = DataStore2("Level", player)
local replicatedDataFolder = Instance.new("Folder")
replicatedDataFolder.Parent = replicatedStorage.ReplicatedData
replicatedDataFolder.Name = player.UserId
---------------------------------------------
--STARTING THE INVENTORY FUNCTION--
local inventoryString = Instance.new("StringValue")
inventoryString.Parent = replicatedDataFolder
inventoryString.Name = "Inventory"
--Leaderstats--
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local LevelValue = Instance.new("IntValue", leaderstats)
LevelValue.Name = "Level"
local expValue = Instance.new("IntValue", leaderstats)
expValue.Name = "Exp"
----------------------------------------------
--Functions--
local function updateLevel(level)
player.leaderstats.Level.Value = level
end
local function updateEXP(exp)
if exp >= expToLeveUp(LevelStore:Get(defaultLevel)) then
ExpStore:Increment(expToLeveUp(LevelStore:Get(defaultLevel)) * -1)
LevelStore:Increment(1)
else
player.leaderstats.Exp.Value = exp
end
end
local inventoryData = InventoryStore:Get({})
inventoryString.Value = HttpService:JSONEncode(inventoryData)
InventoryStore:OnUpdate(function(decodedData)
inventoryString.Value = HttpService:JSONEncode(decodedData)
end)
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)
--call functions right away one time
updateLevel(LevelStore:Get(defaultexp))
updateEXP(ExpStore:Get(defaultexp))
--call function right away one time
local function updateClientLevel(amount)
replicatedStorage.Events.UpdateClientLevel:FireClient(player, amount)
end
LevelStore:OnUpdate(updateLevel)
ExpStore:OnUpdate(updateEXP)
end)
game.Workspace:WaitForChild("Bruh").ClickDetector.MouseClick:Connect(function(player)
local CurrencyStore = DataStore2("Currency", player)
CurrencyStore:Increment(1,DefaultCurrencyAmount)
end)
And the increment script
local ServerStorage = game:GetService("ServerStorage")
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
while true do
wait(3)
for k, player in pairs(game.Players:GetChildren()) do
local expStore = DataStore2("Exp", player)
expStore:Increment(5)
end
end
Did you mean to make this a remoteevent? this code only gets ran once, and there’s nothing that you can do to update it. Make it into a RemoteEvent like what you did for Currency and do what you did for updateClientCurrency
but for the level
So i did this in the datahandler
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
end
and this for the button
local replicatedStorage = game:GetService("ReplicatedStorage")
replicatedStorage.Events.UpdateClientLevel.OnClientEvent:Connect(function(amount)
script.Parent.Text = "Level: " .. amount
end)
Is this correct?
You’re on the right track, change the updateLevel function to
local function updateLevel(level)
replicatedStorage.Events.UpdateClientLevel:FireClient(player,level)
end