Hello scriptinghelpers users. I Have a question How do i make a click detector shop FOR example You click the gun that costs 20 money And the money gets taken from ur leaderboard money. If the player doesnt have number "20" He doesn't get the gun. Please help! If you don't understand just don't answer.
Alright, let's do this. It might be a bit long. But I will explain everything on the way.
First of all, make sure you have a screen gui and inside of that screen gui, a textbutton. We will click this button to get the item. You can customize it the way you want.
Now make sure that your tool you want to give (in this case, it should be the gun) is inside of the replicated storage.
And also create a Remote Event inside the Replicated Storage. Call it "GiveGun"
Inside the text button we created earlier, insert a local script.
local Player = game.Players.LocalPlayer local ReplicatedStorage = game:GetService("ReplicatedStorage") local Price = 50 -- Change it to the price you want the item to be.
Here we have just defined some simple variables.
Now, we will check if the player has clicked on the text button. If they do, we will check if they have enough money, and if they do, we will check if they already have the gun in their backpack (so that they don't spam their backpack). If they don't, then we give them the gun and reduce their money.
So here is the rest of the code:
local Player = game.Players.LocalPlayer local ReplicatedStorage = game:GetService("ReplicatedStorage") local Price = 50 -- Change it to the price you want the item to be. script.Parent.MouseButton1Click:Connect(function() if Player.leaderstats.Money.Value >= Price then -- Change "Money" to the stat you have in leaderstats. if not Player.Backpack:FindFirstChild("Gun") then -- Checking if the player already has the Gun in their backpack. If they don't, then we give them gun. game.ReplicatedStorage.GiveGun:FireServer() -- Firing a Remote Event so that the rest of the processing could be handled by the server. end end end)
Now next, create a script inside the 'ServerScriptService'. Call it anything you want. This will handle the remote event we fired earlier.
Add this inside of it:
game.ReplicatedStorage.GiveGun.OnServerEvent:Connect(function(player) player.leaderstats.Money.Value -= 50 -- Change "50" to the price of the item. game.ReplicatedStorage.Gun:Clone().Parent = player.Backpack game.ReplicatedStorage.Gun:Clone().Parent = player.StarterGear end)
This should successfully give the gun to the player.
If you have any queries, let me know.
I will be glad to help.