I want to make a script in a part that when clicked it charges you gold then you get a tool. I am a bit close to doing this I just need help with some small script segments. How will I make it to charge them, how to make a clone of the tool, and how to put the tool into the players hot bar. The tool I am using is called “Stone Axe” and it is in the server storage. I also have a money leaderboard that is in the server script service. Here is its script (I have 2 types of money):
function onPlayerEntered(player)
local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”
stats.Parent = player
Well, you use local “cash” twice, which may or may not mess up the stats, but I would change one of the them to have a different name.
And for the purchase part when they click the part to buy it (add a ClickDetector into it), you would check if their Gold/ other currency, the amount that they have if it is greater than or equal to the needed amount to buy it. And if so, detract the money from their balance and clone the tool and parent it to the player. And if they don’t have enough money, you could have a gui become visible for a few seconds saying they need more money, and maybe prompt a currency purchase.
This is more of a concept script, but I could probably give you an actual script if you need.
I have 2 different cashes, gold and alexandrite. I don’t need to use alexandrite for this. and I already know I need to put a clickdetector and find out how much gold and stuff. But I would just like 3 small scripts for what I said earlier and then I can put them together and add stuff. Thank you so far.
When Touched,
Fire event,
Check if player has enough gold to buy tool, (on the server)
If he does. Remove x amount of gold from player and give him the tool.
If you have no idea what I just said, I recommend reading the docs and looking at some “shop” tutorials.
--this script goes in the part you click to prompt purchase
part.MouseClick:Connect(function()
purchasegui.Visible = true --you can tween too
end)
Have a script in the decline purchase button that makes the gui not visible, or tween whichever you want.
--this script goes in the buy button
local storage = game:getService("ServerStorage") --unless the tool is in replicated storage
script.Parent.MouseButton1Up:Connect(function(player)
purchasegui.Visible = false -- or tween
player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - x --whatever amount it costs
local toolClone = storage.ToolNameHere:Clone()
toolClone.Parent = player.Backpack
end)
Tell me if there are any errors. (There’s probably one or two, cuz I’m not that great of a scripter.)
When the player presses the buy button. Fire an event, and check his gold and give him the tool on the server. As he can easily just change his gold to 999,999,999 and the client will see that. Making a shop without sanity checks is suicide.
O dear god… first of all, serverstorage can’t be accessed from the client.
Second of all, don’t give client this much authoraty. It is not even going to replicate other than the client that performed the action… So practically useless.
Third of all, rely input on client and then everything else on the server!
In order to clone something you would use the Clone method, here’s an example
local Part = game.workspace.Part
Part.Anchored = false
local PartClone = Part:Clone() -- Create a clone of Part and assign it to a variable called PartClone
PartClone.Anchored = true -- Anchor the clone
print(Part.Anchored) -- false
print(PartClone.Anchored) -- true
And if you want to put a tool on someone’s hotbar, you parent it to someone’s Backpack which is a folder that’s parented to their player. Example:
local Backpack = Player.Backpack
Tool.Parent = Backpack
You shouldn’t assign Gold to cash and then assign Alexandrite to cash, you should use different variables for each of them.
And if you want to check if your gold is more than a certain number, you would check its value property.
So the finished product:
if Gold.Value > YourPrice then --Or if this is in a different script, you would do Player.leaderstats.Gold instead of Gold because the variable Gold won't be accessible there
Gold.Value -= YourPrice -- Subtract the price from your gold
local Tool = game.ServerStorage["Stone Axe"]:Clone()
Tool.Parent = Player.Backpack
end