Ingame currency / Coins

  1. What do you want to achieve?
    So im making an obby type game, where the user can collect coins on each stage, although, i am having trouble creating a system where the user can spend these coins in a shop located at the spawn of the game which they can teleport to.

  2. What is the issue?
    unable to find a script or any idea how to link a coin model to a functioning ingame currency script

  3. What solutions have you tried so far?
    I’ve looked at 20+ youtube videos, none of them really cover the idea i have and really give no real guide.

2 Likes

You can make the currency script connect to the coins to Touched events! You would have to find all the coins in your game and do it. The Touched events would use your currency related functions to add the coins to the player (since your code could reside in the same script). You could also use a ModuleScript to allow multiple scripts access the currency script. Make sure to add a debounce too!

I think he means that he already has the coin collecting system, but he’s not sure how to let the user actually spend those coins in the shop.

1 Like

Assuming you mean that you don’t know how to use the coins in the shop, then you would want to check the players coin value when a buy button is clicked, then if the player has enough fire a remote event that makes the server confirm they have enough, subtract the price and give the player the item.

2 Likes

To add to that, the check that the player has enough currency should be done on the server because an exploiter doesn’t need to go through the process of clicking that button.

Client clicks button > Fire remote event

Server checks if the client has sufficient currency

yes: subtract currency from client, give player item
no: fire client > client creates a message on screen displaying that the user has insufficient currency

Never trust the client.

I personally check first on the client, the. if they don’t have enough I display a notification, if they do, I fire to the server, checking again on the server, and then subtracting and giving if they do.

I find this easier then using remote functions go client to server back to client.

1 Like

Um so here is the code for the coin
local db = true
script.Parent.Touched:Connect (function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) ~= nil then
if db == true then
db = false
script.Parent.Transparency=1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value+12
script.Sound:Play()
wait(1)
script.Parent:Remove()
end
end
end)

And this is the code for the leaderboard
game.Players.PlayerAdded:connect(function(plr)
local f = Instance.new(“Folder”, plr)
f.Name = “leaderstats”
local coins = Instance.new(“IntValue”, f)
coins.Name = “Coins”
coins.Value = 0
end)

Basically i want the player to walk up to a shop at spawn, and be able to purchase like accessories or boosters etc etc. im really not sure how id do that. Sorry if this is like a really simple thing to do.

In future work please use :Destroy() as Remove is deprecated

For the shop you could have a GUI, and when a user presses a button on the GUI it sends a Remote to the server and then the server checks how much money the user has and either gives them or doesn’t give them the item.

Useful Links (I am taking you as a beginner apologies if you know this stuff)
Remotes
GUI Intro

2 Likes

Actually, it might not be that simple. Coincidentally I’m working on currency right now too! I recently did currency with leaderstats. My main problem was accessing the player from the character and you have found a way! This should be quite easy now. The code can be like this:

game.Workspace.Shop.Hat.ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
      if PlayerWhoClicked.leaderstats.Coins.Value>= "Price of hat" then
      PlayerWhoClicked.leaderstats.Coins.Value=PlayerWhoClicked.leaderstats.Coins.Value-"Price of Hat"
      game.Workspace.Shop.Hat.Parent=PlayerWhoClicked
      end)
end

Thanks for bringing up the fact that the MouseClick event of the click detector returns the player!

Wouldn’t you clone the object into the player?
workspace.Shop.Hat:Clone().Parent = PlayerWhoClicked

2 Likes

Oh yeah right. I forgot lol. Thanks for telling me.

3 Likes