How to give the player a tool

Hi,

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

local cash = Instance.new("IntValue")
cash.Name = "Gold"
cash.Value = 0
cash.Parent = stats

local cash = Instance.new("IntValue")
cash.Name = "Alexandrite"
cash.Value = 0
cash.Parent = stats

end

game.Players.ChildAdded:Connect(onPlayerEntered)

Thank you!

3 Likes

Is it a gui or something else?

1 Like

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.

1 Like

button_Clicked → event from client to server → compare player’s stats: 1. buy, 2. decline.

1 → give tool to player’s backpack

Now that’s the logic behind the code.

2 Likes

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.

1 Like

When you click on the part do you want it to show a purchase gui where you can buy it or automatically do it?

1 Like

what do you mean, the thing I want to click to activate the script is a part.

1 Like

a screen gui like that would be nice.

1 Like

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.

1 Like

that won’t mess up anything lol

1 Like

what docs? but I think I will try some videos

1 Like
--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.)

2 Likes

You’d want to use sanity checks,

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.

1 Like

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!

Ahhhh sorry I’m learning, but thanks for telling me!
smh me

1 Like

Everyone appreciates you trying, if you ever need help, message me :slight_smile:

But yeah, it is important that you learn the fundamental things well before you teach anyone anything. Network communication is a huge part of it.

2 Likes

If you are a beginner then don’t worry, anti cheats and how to secure your game/etc is something you should learn later on.

1 Like

._. This isn’t even about securing your game, it is fundamental replication.

1 Like

I was talking about sanity checks :expressionless:

1 Like

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.

local Gold= Instance.new("IntValue")
Gold.Name = "Gold"
Gold.Value = 0
Gold.Parent = stats

local Alexandrite= Instance.new("IntValue")
Alexandrite.Name = "Alexandrite"
Alexandrite.Value = 0
Alexandrite.Parent = stats

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
3 Likes