My code:
01 | function onEnter(player) |
02 |
03 | local ld = Instance.new( "IntValue" ) |
04 | ld.Parent = player |
05 | ld.Name = "leaderstats" |
06 | local value 1 = Instance.new( "IntValue" ) |
07 | value 1. Parent = ld |
08 | value 1. Name = "Points" |
09 | value 1. Value = game.DataStoreService:Get |
10 |
11 | end |
12 |
13 | game.Players.PlayerAdded:connect(onEnter) |
14 |
15 | for i,v in pairs (game.Players:GetChildren()) do -- So that it works in Play Solo. |
16 | onEnter(v) |
17 | end |
Writing a currency system. Really don't want to make a gui just for "points" but will if need be.
If anyone knows why it won't work, I'd love to know.
Ran in Studio.
You should use this as it creates the stat and saves it. Also, game.DataStoreService:Get
isn't a thing.
01 | local DataStore = game:GetService( "DataStoreService" ) |
02 | local ds = DataStore:GetDataStore( "PointsSave" ) |
03 | game.Players.PlayerAdded:connect( function (player) |
04 | local leader = Instance.new( "Folder" ,player) |
05 | leader.Name = "leaderstats" |
06 | local Cash = Instance.new( "IntValue" ,leader) |
07 | Cash.Name = "Points" --Name of your leaderstat |
08 | Cash.Value = ds:GetAsync(player.UserId) or 100 --Change to how much points the player will start out with |
09 | ds:SetAsync(player.UserId, Cash.Value) |
10 | Cash.Changed:connect( function () |
11 | ds:SetAsync(player.UserId, Cash.Value) |
12 | end ) |
13 | end ) |
14 |
15 |
16 | game.Players.PlayerRemoving:connect( function (player) |
17 | ds:SetAsync(player.UserId, player.leaderstats.Points.Value) |
18 | end ) |
This script should go in the ServerScriptService
.
If this helped, please upvote and accept answer.
Just put .Parent before .Name
1 | local ld = Instance.new( "IntValue" ) |
2 | ld.Name = "leaderstats" |
3 | ld.Parent = player |
Do that with every other stat too