Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to I make a code that when I click a part, it gives the player a sword and takes away 10 money?

Asked by 3 years ago
Edited by Gey4Jesus69 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

The code gives the sword to the player but it wont take the money away. Here is the code.

local click_detector = Instance.new('ClickDetector', script.Parent)
Sword = game.Workspace.ClassicSword
money = game.Players.LocalPlayer.Currency.IntValue

click_detector.MouseClick:connect(function(player)
    local newTool = Sword:Clone()
    newTool.Parent = player:FindFirstChild("Backpack")
    money = money - 10
end)

1 answer

Log in to vote
0
Answered by 3 years ago

There's quite a bit of common sense that goes into what you're trying to do.

First, you need to check if the player's money is sufficient enough to actually acquire the sword. Once they have bought the sword they can equip and unequip it as they please without needing to repeatedly pay the money needed to use the sword.

If it is actually taking money and it's not indicating that the value changed (which there is none in your code), you need to indicate it. After subtracting the money required to use the sword from the player's balance, you can print the value of their balance or display the balance in a GUI (or both).

Also, it appears as if you're using a LocalScript. Due to FE, the server will not read changes made on the client to the value. The server needs to change the value itself. Now, this is not to say that you can't get a player on the server, of course you can. Logically, if your store is a GUI, you may need to use a RemoteEvent or RemoteFunction. If it's something along the lines of a ClickDetector, the server can get the player who clicked on the detector via that detector's MouseClick event. If neither of these are being done, your best two options are Players:GetPlayerFromCharacter() and Players.PlayerAdded. That's all I'm going to say about what you should do.

Ad

Answer this question