How do I reference a currency from a leaderstats script to a Shop script

I dont know how to reference gold to a shop script and I need help! I know I dont have data saving yet but I need to know this! Thank you for your time.

local players = game:GetService("Players")

local function onPlayerJoin(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
	
	
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
10 Likes

Inside of your shop script you will need to get the Player and then you can reference the gold by doing Player.leaderstats.Gold.

For example, if you wanted to subtract some gold you could do something like
Player.leaderstats.Gold.Value -= cost

2 Likes

so is it game:Getservice(“Players”)

2 Likes

it sounds a little vague, is there a way I can get any specific player and do this?

1 Like

Assuming your store is a UI and the script you are refering to is a server script you could possibly place the script in StarterGui and refer to the player by finding the first ancestor of class Player.
script:FindFirstAncestorWhichIsA("Player")

2 Likes

This would be messy since the script would be replicated to all players. You could just do Players:FindFirstChild(playerName)

1 Like

But how would you get the playerName without already knowing the player?

1 Like

wouldnt the shop if it was a serverscript be broadcasted to all players so if someone buys something it would give it to everyone, etc.

1 Like

Based on the poster’s usage of getting a specific player, I’d assume they would already know the player, and if not, there’s a pretty big issue with how the system is being handled.

Gold is being stored in every player

No. You control what happens in a Server Script, so you would have to specifically program it to do so.

I assumed, but this isn’t an issue. What’s the use case for this? There shouldn’t be an issue knowing the target player in any normal situations.

so I want to know how to make a shop but I had trouble figuring out how to get gold from my other leaderstat to the shop, I’m a fairly new scripter so I’m sorry if I don’t understand things

1 Like

No problem! If you’re making a shop system, the logic for starting the purchases should be handled on the Client. Then, you can get the client to fire an event on the server, thus giving the Server the player object, where you can check the gold values and confirm the purchase safely.

could you give me an example of that? it doesnt have to be a whole script just tryna wrap my head around making something for a client and firing an event

You would need 2 scripts. A local script & a server script. Put a RemoteEvent inside the same area as the Server Script & Local Script.

-- Server Script
local RemoteEvent = script.Parent.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(player,cost)
	local Gold = player.leaderstats.Gold
	if Gold.Value >= cost then
		Gold.Value -= cost
	end
end)
-- Local Script
local RemoteEvent = script.Parent.RemoteEvent

-- Example Button
script.Parent.TextButton.Activated:Connect(function()
	RemoteEvent:FireServer(1) -- 1 refering to cost
end)

No problem. I’m not in Studio right now so this is more pseudo code and examples than anything but here’s what you could do.
You use RemoteEvents (put in ReplicatedStorage or something both the client and server can access) for the event functionality I was talking about, and make a reference for the event on the client and the server.
On the client, when you know the player wants to make a purchase (by clicking a buy button or something like that) you could fire the event by doing this (in a local script)
EventObject:FireServer("Sword")
and putting details, like the name of the purchase or stuff like that, as a parameter in parentheses (not required).
Then, connect this to a server script by doing something like this (in the server script):

EventObject.OnServerEvent:Connect(function(player, itemName)
-- Now you've got the player and the item name!
end)

The player parameter shown in this example is completely automatic and done by Roblox (you don’t put it as a parameter when firing the event). With this, you could check the player’s leaderstats from the function!

Be careful when you do this. ServerScriptService isn’t replicated (accessible) to the client for security reasons, meaning it wouldn’t be able to access the event if put there.

Also, you should handle sensitive details like cost on the server instead of the client because the player could fire the event through exploits to purchase an item for free or even negative costs, which could give them infinite gold.

I only used a local script because you suggested it. As I was mentioning earlier you could do the entire thing on a serverscript without the need for a local script in the first place

Yes but this approach is much messier and can create issues if done with the method you described. A good LocalScript to ServerScript connection can make writing and managing things much easier without the weirdness of replicated ServerScripts, which you should definitely try to avoid