local rp = game:GetService("ReplicatedStorage") -- Service for replicating objects between client and server
local remotes = rp:WaitForChild("Remotes") -- The Remotes folder in the replicated storage
local player = game:GetService("Players").LocalPlayer -- Local player
local formatNumber = require(rp:WaitForChild("Libs").FormatNumber.Simple) -- Module for number formatting
local gui = player.PlayerGui:WaitForChild("MainGui") -- The player's main GUI interface
-- Function to update currency display
local function updateCurrency(currency: "BunkCoins" | "Coins", amount)
-- Format the number into a readable format
--amount = formatNumber.FormatCompact(amount)
-- If the currency is BunkCoins, update the text in the main interface
if currency == "BunkCoins" then
gui:WaitForChild("TextLabel").Text = amount
end
end
-- Update the display of BunkCoins when the game loads
updateCurrency("BunkCoins", remotes.GetData:InvokeServer("BunkCoins"))
-- Listen for the event of updating the amount from the server and update the display
remotes.UpdateCoinEvent.OnClientEvent:Connect(function(amount)
updateCurrency("BunkCoins", amount)
end)
Module:
-- Function to adjust the amount of BunkCoins when clicked
function Manager.AdjustBunkCoins(player: Player, amount: number)
local profile = Manager.Profiles[player]
-- If the player profile doesn't exist, exit the function
if not profile then return end
-- Increase the BunkCoins amount by the specified value
profile.Data.BunkCoins += amount
print(profile, profile.Data, profile.Data.BunkCoins, amount)
-- Notify the client about the change in BunkCoins amount
remotes.UpdateCoinEvent:FireClient(player, profile.Data.Coins)
end
So Iβm making a clicker based on the tutorial:
and this is my script for displaying clicks (coins), the problem is that, sometimes, once in a couple times when entering the game there is this bug, writes an error:
local rp = game:GetService("ReplicatedStorage") -- Replicated storage service
local remotes = rp:WaitForChild("Remotes") -- Folder containing remote events
-- Create a table to store player profiles
local Manager = {}
Manager.Profiles = {}
-- Function to adjust the amount of BunkCoins when clicked
function Manager.AdjustBunkCoins(player: Player, amount: number)
local profile = Manager.Profiles[player]
-- If player profile doesn't exist, exit the function
if not profile then return end
-- Increase the BunkCoins amount by the specified value
profile.Data.BunkCoins += amount
print(profile.Data.BunkCoins)
-- Notify the client about the change in BunkCoins amount
remotes.UpdateCoinEvent:FireClient(player, profile.Data.Coins)
end
-- Function to get data from player's profile
local function getData(player: Player, directory: string)
local profile = Manager.Profiles[player]
-- If player profile doesn't exist, exit the function
if not profile then return end
-- Return data from the specified directory in player's profile
return profile.Data[directory]
end
-- Assign getData function as a callback for remote invocation
remotes.GetData.OnServerInvoke = getData
return Manager
Maybe the player does not have a profile yet, and it returns nil. See if the profile exists or not with a simple print(profile) before the profile check.
local function getData(player: Player, directory: string)
local profile = Manager.Profiles[player]
if not profile then return end
print(profile)
return profile.Data[directory]
end
local function getData(player: Player, directory: string)
local profile = Manager.Profiles[player]
print(profile)
if not profile then return end
return profile.Data[directory]
end
hmm, okay, but first of all Iβm sure they exist, and the problem is that sometimes do not have time to create or something like that, because Iβm testing now and out of 8 attempts (restarts) this bug is not there.
-- Function to adjust the amount of BunkCoins when clicked
function Manager.AdjustBunkCoins(player: Player, amount: number)
local profile = Manager.Profiles[player]
-- If the player profile doesn't exist, exit the function
if not profile then return end
-- Increase the BunkCoins amount by the specified value
profile.Data.BunkCoins += amount
print(profile, profile.Data, profile.Data.BunkCoins, amount)
-- Notify the client about the change in BunkCoins amount
remotes.UpdateCoinEvent:FireClient(player, profile.Data.BunkCoins)
end