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

How do i make a buy sign for my game?

Asked by 3 years ago

Hi i'm currently working on my zombies game mode and would like to put in a buy sign for guns. Meaning that when you click on the sign the sign checks if you already have the tool in the backpack and if you do it does nothing. But if you dont have the tool in the backpack it subtracts the price from your money and clones it into your backpack. I tried almost everything i have no idea anymore how i am supposed to implement it

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You really need to provide some more information, at least explain to us how you handle economy, where your guns are stored, etc. I will try to explain using my own hierarchy and stats, and hopefully you can implement it into your game, or at least learn from it.

--Inside a script located anywhere.

--First of all lets create the remote event so we can detect when the player hits the button. You may also create this yourself by making a folder named "Remotes" inside rep storage, and a remote event inside that named "BuyGun"
local Remotes = Instance.new("Folder")
Remotes.Name = "Remotes"
Remotes.Parent = game.ReplicatedStorage
local BuyGun = Instance.new("RemoteEvent")
BuyGun.Name = "BuyGun"
BuyGun.Parent = Remotes


game.Players.PlayerAdded:Connect(function(player) -- Lets detect when the players join so we may give them cash. If you have a system for cash already, then edit the below lines to direct to your players cash.
    local stats = Instance.new("Folder")
    stats.Name = "Stats"
    stats.Parent = player
    local Cash = Instance.new("NumberValue")
    Cash.Name = "Cash"
    Cash.Value = 100
    Cash.Parent = stats
end)
local guns = game.ReplicatedStorage.Guns -- This is the location of your guns that you want replicated to your player.
local function GiveGun(player,gun) -- Lets go ahead and just make our code into a function so we can call it later with ease.
    local hasgun = player.Backpack:FindFirstChild(gun.Name) -- Check to see if the player has the gun already. If they have the gun in their hand, it will be in their characters model, not backpack, so check that too.
    if hasgun == nil then
        hasgun = player.Character:FindFirstChild(gun.Name) -- Check players character for gun.
    end
    if hasgun == nil then -- If the player doesn't have the gun, then continue.
        if player.Stats.Cash.Value >= gun.Price.Value then -- Not sure where you store your players cash, but I usually store mine in a folder named "Stats" parented to the player. This line checks to see if the player has enough money to buy the gun. For simplicity I assume there is a numbervalue inside the gun named "Price" and that will determine how much it is.
            player.Stats.Cash.Value = player.Stats.Cash.Value - gun.Price.Value -- If player had enough money, subtract the guns price from the players cash.
            local playergun = gun:Clone() -- Create the new gun. You want to avoid using the model itself so you can keep reusing it, hence why we clone it.
            playergun.Parent = player.Backpack -- Parent the new gun to the players backpack.
        end
    end
end
game.ReplicatedStorage.Remotes.BuyGun.OnServerEvent:Connect(GiveGun)




--Inside a local script located in StarterGui

for i,v in pairs(script.Parent.ScreenGui:GetChildren()) do -- Lets go ahead and gather all of our gui buttons. Using Get Children is a good way, just make sure your buttons are isolated so you dont have mishaps.
    if v:IsA("TextButton") then  -- If the child is infact a text button, run below code
        v.MouseButton1Click:Connect(function() -- If player clicks the button, run below code
            if game.Players.LocalPlayer.Stats.Cash.Value >= v.GunToBuy.Value.Price.Value then -- Check to see if player can afford it first. As this is in a local script, this wont prevent exploiters, check to make sure they actually have the money via server script to insure they are spoofing money. This just prevents needless remote spam.
                game.ReplicatedStorage.Remotes.BuyGun:FireServer(v.GunToBuy.Value) -- Fire remote to see if player can have the gun
            end 
        end)
    end
end

I have uploaded a picture of my heirarchy to help you understand it better. You want a screengui inside starter gui. Place a text button inside that screengui. Place an objectvalue inside the text button. Set the objectvalues.Value to the gun you want it to be responsible for. When the player clicks the button it'll give them the gun aslong as they dont have one, and can afford one. https://prntscr.com/w74cgo

0
Why would you provide a script for him? You're essentially giving him a script. I know you're nice and all, but you need to make sure that it isn't a request. Dovydas1118 1495 — 3y
0
Hi! Thanks for the script but i juust figured it out. I just made a ClickDetector and connected it to the script ledevinx -5 — 3y
0
The only reason I provide codes is in hopes that they will learn from it. I myself learned most of my coding from breaking down and analyzing scripts. Sure he may just copy-paste this code into his game and call it day, but if even the 1 learns from it, it'll be worth it. WizyTheNinja 834 — 3y
Ad

Answer this question