Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

The start of my 'Money' system is not working correctly, any help?

game.Players.PlayerAdded:connect(function(plr)
local money = Instance.new("IntValue", game.Players.LocalPlayer)
money.Value = 50	
end)

I can't seem to figure out why this isn't working, maybe I need to user leaderstats? But I didn't think that was needed as I just need a singular intvalue to define how much money the user currently has.

1 comment

0

what exactly isnt working?

2 answers

✓ Accepted answer

Well, here it is hope this helps.

First off, a server script can't access the LocalPlayer object. Also that's not a good way to go about doing this.

I'm also adding DataStore for you so it saves it! You're welcome! XD

So do this in a Server script


local startingCashValue = 0 -- Set this to what you want the player to start out with

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("MoneyDataStore")
game.Players.PlayerAdded:connect(function(plr)

local leader = Instance.new("Folder")

local cash = Instance.new("IntValue") 

leader.Name = "leaderstats"
leader.Parent = plr

cash.Name = "Money" 
cash.Parent = leader

cash.Value = ds1:GetAsync(player.UserId) or startingCashValue



end)

game.Players.PlayerRemoving:connect(function(player)
		print("Saving Data For "..player.Name.."..")
		ds1:SetAsync(player.UserId, player.leaderstats.Points.Value)
		print("Data For "..player.Name.." has been saved!")
end)

2 comments

0

It still seems to not work, it isn't creating the leaderstats or the intvalue.

0

Hmm, that's odd my script is working on my end..

If this is a normal script you cannot use game.Players.LocalPlayer

But that is okay because when you set up

game.Players.PlayerAdded:connect(function(plr)

you made the parameter plr

you can just do this instead

local money = Instance.new("IntValue", plr) -- use plr here
money.Value = 50  

2 comments

1

Also note that the parent argument in Instance.new can cause performance issues. Set the parent separately. (See https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296)

0

I was using a LocalScript but thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.