Personal variable

Hi! I would like know how to make a personal variable like money. Can you help me please?

1 Like

What do you mean by a “personal variable”, there are global variables for module scripts and _G (I dont recommend _G because the variable can be changed and sometimes scripts load before the variable, erroring it)

1 Like

I must add a variable in a module script?

I am trying to say, what do you mean by a “personal variable”, do you mean leaderstats?

A personal variable is a variable which belongs to a player. For example, the money will not be the same for everyone.

_G is a global variables, modules can be used, but if you are trying to make a custom variable, oop (Object Oriented Programming) is the way to go.

if you are looking for a way to do this and show it, here you go

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player); leaderstats.Name = "leaderstats"

local cash = Instance.new("IntValue", leaderstats); cash.Name = "cash"

end

do you mean leaderstats, if so, you have to create a folder in the player called leaderstats (same capitilization)

I must write this on a module script or a normal script? And does it save player data?

Try searching for a tutorial on it. Like this one, I’m pretty sure you mean a saving leaderboard. The tutorial is a bit outdated though. If it doesn’t help look for another one.

1 Like

Their’s no “personal variable” variables are independent from the player instance. The thing is we need features which are versatile rather then specific to one case, and that’s why we got hashtables

To make something like it you would use a table/hashtable

local MoneyTable = {

}

game.Players.PlayerAdded:Connect(function(Player)

MoneyTable[Player] = 0

end)

game.Players.PlayerRemoving:Connect(function(Player)

MoneyTable[Player] = nil

end)

You don’t have to use a module script, but a module script will allow you to acess this data in any script, while in a nomal script do this you would have to do _G and a lot of people think it’s antipattern.

However if your just looking to use this data in one script then use a table inside a normal script.

I watched the video but how is the variable used? (By example, how to add 1 this variable with another script?)

Lets say you want the value to go up by one whenever the player clicks a block, you would type something like:

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
    playerWhoClicked.leaderstats.Points.Value + = 1 --This is how much points it adds
    --make sure to replace "Points" with whatever the value is called
end)

by the way i never tried the Value + = 1 way yet, so i dont know if that works.

can you please explain what you mean by “personal variable”, do you mean leaderboards (leaderstats) like the “Money” stat in Jailbreak

What I assume you want is to tie a value to a player.

You should use ValueBases for this, namely IntValue or NumberValue, you should then parent it to the player so that you can automatically tie it to the player

If you name a Folder or Model ‘leaderstats’ then parent it to a player, it will appear in the leaderboard

local Players = game:GetService("Players")

local function PlayerAdded(plr)
  local leaderstats = Instance.new("Folder")
  leaderstats.Name = "leaderstats"
  
  local money = Instance.new("NumberValue")
  money.Name = "Money"
  money.Value = 0
  money.Parent = leaderstats  

  leaderstats.Parent = plr
end)

Players.PlayerAdded:Connect(PlayerAdded)

Since this is binding an event, you’d preferably want this in a regular old script.
But now, (as long as you do it on the server), you have created a Money value that can be easily changed by doing

Players.(player name).leaderstats.Money.Value = (value)


TLDR @scripting1st: PSA: Don't use Instance.new() with parent argument

2 Likes

PlayerAdded can run after player/s got into the game, so good practice is to loop Players service and attach PlayerAdded event to them.

How to avoid that the variable is displayed in the chat like in this picture?

Captureeza

That’s what leaderstats do, if you don’t want it use tables.