Buying and Equipping Tools From A Shop

server script:

local purchase = game.ReplicatedStorage:WaitForChild("PurchaseEvent")

purchase.OnServerEvent:connect(function(player, amount, item)
if player.leaderstats.Cash.Value >= amount and not player.Character:FindFirstChild(item) and not     player.Backpack:FindFirstChild(item) then
    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - amount
    game.ReplicatedStorage:FindFirstChild(item):Clone().Parent = player.ownedtools

end
end)

game.Players.PlayerAdded:Connect(function(player)
local ownedtools = Instance.new("Folder", player)
ownedtools.Name = "ownedtools"
end)

I redid some parts of the local script to fit what you added

Local Script
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local cash = player.leaderstats.Cash
local purchase = game.ReplicatedStorage:WaitForChild("PurchaseEvent")
local cameras = workspace.Cameras


script.Parent.BuyButton.MouseButton1Click:Connect(function()
  for i, v in pairs(cameras:GetChildren()) do
if camera.CFrame == v.CFrame and not player.ownedtools:FindFirstChild(v.Toolname.Value) then 
    purchase:FireServer(v.Cost.Value, v.Toolname.Value)
	script.Parent.BuyButton.Visible = false
	script.Parent.Equip.Visible = true
        --Make sure "Equip" is a textButton or imageButton
        script.Parent.Equip.MouseButton1Click:connect(function()
                  if script.Parent.Equip.Text ~= "Equipped" then
                      script.Parent.Equip.Text = "Equipped"
                      script.Parent.Equip.BackgroundColor3 = BrickColor.Red().Color
                      game.ReplicatedStorage.toolEquipper:FireServer(v.Toolname, true)
                 else
                        script.Parent.Equip.Text = "Equip"  
                        script.Parent.BackgroundColor3 = BrickColor.Green().Color
                        game.ReplicatedStorage.toolEquipper:FireServer(v.Toolname, false)  
                 end
        end)
    end
  end
end)

I added the equip function that I mentioned previously to the server script

Server Script
local purchase = game.ReplicatedStorage:WaitForChild("PurchaseEvent")
local equip = game.ReplicatedStorage:WaitForChild("toolEquipper")

purchase.OnServerEvent:connect(function(player, amount, item)
if player.leaderstats.Cash.Value >= amount and not player.Character:FindFirstChild(item) and not     player.Backpack:FindFirstChild(item) then
    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - amount
    game.ReplicatedStorage:FindFirstChild(item):Clone().Parent = player.ownedtools

end
end)

game.Players.PlayerAdded:Connect(function(player)
local ownedtools = Instance.new("Folder", player)
ownedtools.Name = "ownedtools"
end)

equip.OnServerEvent:connect(function(player, tool, equip)
    if equip then --Equip is true so lets equip the tool
        if player.Backpack:FindFirstChild(tool) then -- if the tool is inside the backpack then continue
           --lets unequip the other tools
           for i, v in pairs(player.Character:GetChildren()) do
               if v:IsA("Tool") then
                   player.Character.Humanoid:UnequipTools(v)
               end
           end
            --this will equip the players tool from their backpack
            player.Character.Humanoid:EquipTool(tool)
        end
    else --They dont want to equip this tool so lets unequip it 
        --Unequip the tool
        for i, v in pairs(player.Character:GetChildren()) do -- since the player can only have 1 tool at a time, we can still use this just to make sure all the tools are unequipped.
            if v:IsA("Tool") then
               player.Character.Humanoid:UnequipTools(v)
            end
        end
    end
end)
1 Like

My solution to the problem of getting only 1 item equipped would just be to make a folder/table or some sort (ex. a folder on the server that’s unique to the player, you could name them by concatenating a word to their UserId.) that contains the tool that the player has equipped, and then clone whatever’s in there to the player’s backpack. That way, you can do a condition that checks if a tool is in that folder and if it is, it deletes the tool from the backpack and the folder and switches it out for the new tool, which then gets cloned to the player’s backpack.

Also, you’d have to track what a player owns, (for example by using boolvalues for each tool, which is what I do).

So, for example, you could make a Folder in ServerStorage named PlayerTools, and on a PlayerAdded event make a new folder like this:



game.Players.PlayerAdded:Connect(function(player)

local folder =  Instance.new("Folder", game.ServerStorage.PlayerTools)
folder.Name = "Player_" .. player.UserId
-- then you can use datastores to track what a player owns and what they have equipped and put the equipped item in this folder 
end)

Sorry for lack of indentation I wrote this out here

Basically all I’m saying is that to solve this problem you just need to store and track what a player owns and what they have equipped

I have something like this already in my system, but I just can get it to work. Everything is kind of jumbled and makes no sense to me.

Can you walk me through some of your methods? (Such as what goes where)
If you need me to show you what I have, I can paste my scripts and provide screenshots, because I also will need datastores later on down the line, which I have no idea how to do.

Yeah sure, I’m working on a project rn I’ll be back soon

I apologize in advance for not describing it thoroughly enough, learning this comes through a lot of trial and error. You must use remote events in order to keep your tools and cash secure. I redid the 2 scripts you sent, all you would need to do is copy and paste the code, and change the “Equip” into a textbutton or an image button. If those didn’t help the only other way I could show you is via teamcreate or via discord to give you a more in depth understanding.

equip was already a text button, and there was a text label named equipped. My original plan was after an item was purchased, the buy button does visible = false and the equip button does visible = true. Then, once an item was equipped, the equip button would do visible = false, and the equipped label would do visible = true.

The problem is, something isn’t working with the equipping. The weapons are purchased and put into the folder, but then when you equip nothing gets placed into the backpack.

oh I forgot to add that, my bad:

game.ReplicatedStorage:FindFirstChild(item):Clone().Parent = player.ownedtools
game.ReplicatedStorage:FindFirstChild(item):Clone().Parent = player.Backpack

I think that was the issue.
Unless you wanted it to be equipped via the button only.
The equip and unequip button should work, if you would rather it clone into the player instead of using the :EquipTools() function then change this in the server script;

Change this in the Server Script
equip.OnServerEvent:connect(function(player, tool, equip)
    if equip then --Equip is true so lets equip the tool
        if player.owneditems:FindFirstChild(tool) then -- if the tool is inside the backpack then continue
           --lets unequip the other tools
           for i, v in pairs(player.Character:GetChildren()) do
               if v:IsA("Tool") then
                   v:Destroy()
               end
           end
            --this will equip the players tool from their backpack
            player.owneditems:FindFirstChild(tool):Clone().Parent = player.Character
        end
    else --They dont want to equip this tool so lets unequip it 
        --Unequip the tool
        for i, v in pairs(player.Character:GetChildren()) do -- since the player can only have 1 tool at a time, we can still use this just to make sure all the tools are unequipped.
            if v:IsA("Tool") then
               v:Destroy()
            end
        end
    end
end)

I made it work with the owned items.

it doesn’t seem to be working for me, and I am not getting any errors in the output.

Is it possible to record a video of what happening?

i can explain what happens:

  1. The player clicks the buy button and the tool is put into the folder.
  2. The player clicks the equip button and the button turns red and says equipped
  3. No tool is equipped
equip.OnServerEvent:connect(function(player, tool, equip)
warn(tool, equip) -- remove this at any point
    if equip == true then --Equip is true so lets equip the tool
        if player.owneditems:FindFirstChild(tool) then -- if the tool is inside the backpack then continue
           --lets unequip the other tools
           for i, v in pairs(player.Character:GetChildren()) do
               if v:IsA("Tool") then
                   v:Destroy()
               end
           end
            --this will equip the players tool from their backpack
            player.owneditems:FindFirstChild(tool):Clone().Parent = player.Character
             warn("Equipped Tool!") -- remove this at any point
        end
    else --They dont want to equip this tool so lets unequip it 
        --Unequip the tool
        for i, v in pairs(player.Character:GetChildren()) do 
            if v:IsA("Tool") then
               v:Destroy()
            end
        end
       warn("Unequipped Tool!") -- remove this at any point
    end
end)

it prints out Toolname True, because equip is true but where was toolname identified?

Ohhhhh it was so simple! I forgot to add Toolname.Value!

script.Parent.Equip.MouseButton1Click:connect(function()
                  if script.Parent.Equip.Text ~= "Equipped" then
                      script.Parent.Equip.Text = "Equipped"
                      script.Parent.Equip.BackgroundColor3 = BrickColor.Red().Color
                      game.ReplicatedStorage.toolEquipper:FireServer(v.Toolname.Value, true)
                 else
                        script.Parent.Equip.Text = "Equip"  
                        script.Parent.BackgroundColor3 = BrickColor.Green().Color
                        game.ReplicatedStorage.toolEquipper:FireServer(v.Toolname.Value, false)  
                 end
        end)

I fixed it right before you replied, and what I did was changed where it said just tool in the server script to tool.value. This makes it so you can equip an item, but then the buy/equip/equipped GUI is screwed up for the rest of the shop

Didnt read that my bad, The equip/unequip doesnt work.

Like I said in my previous post, the GUI only works once to buy and equip a tool. Then it is stuck on equipped, even if I go through to other items. How do I add different weapons and still make it work?
Edit: It works for the other tools, just you can only do one. How can I reset the GUI when I move to a different camera?

I am going to copy and paste the current code and recreate in my studio, I will either tell you whats wrong or I can give you the place so you can just take the scripts whole.

edit: need to make a few changes, I am going to make the button handler control all buttons.

my style of shop is a little bit different, so you can test it for yourself. It should be published to the most recent version. It will be under loadout, and the sword and longsword are the only ones that “work.” The dagger (first item) was going to be a free item and what you get first.