Buying and Equipping Tools From A Shop

just now realizing, you should’ve have your buy button script inside of your camera manipulation script in order to get the camera that is active. I need to modify it, there is no other way you could detect what camera is currently active through a separate script.

Im going to be on tomorrow so I can help you then,

my discord is Odawg566#9616 if you want to pm me

Okay, so me and @Odawg566 talked on discord and we still couldn’t find a solution to this shop yet. I need help to identify a problem in this code and how to fix it. The error that occurs is hard to describe, so you can try it out for yourself here. epic fighting simulator - Roblox
It allows you to purchase one weapon and equip it and unequip it, but then it breaks if you try to unlock anything else. If you can spot an error in the code, please let me know. I can provide screenshots of the explorer tab as well if you need to know where something is located. (Also, preformatted text doesn’t like it when I copy and paste things, so I apologize for that.)
Thanks in advance!

CLIENT SCRIPT
local player = game.Players.LocalPlayer
local player_camera = workspace.CurrentCamera
local cameras = workspace.Cameras
local cash = player.leaderstats.Cash
local camera_count = #cameras:GetChildren()
local purchase = game.ReplicatedStorage:WaitForChild(“PurchaseEvent”)
local cameras = workspace.Cameras
local BUY_BUTTON = script.Parent.BuyButton
local EQUIP_BUTTON = script.Parent.Equip
local current_camera = 1
local equippedTool
local is_tweening

local tween_service = game:GetService(“TweenService”)
local tween_info = TweenInfo.new(0.35, Enum.EasingStyle.Linear)

function updateButton(thecamera)
if equippedTool == thecamera.Toolname.Value and player.ownedtools:FindFirstChild(thecamera.Toolname.Value) and not player.Backpack:FindFirstChild(thecamera.Toolname.Value) then --if (weapon name) is the same as the camera’s corresponding tool and is in the player’s ownedtools then
EQUIP_BUTTON.Visible = true
BUY_BUTTON.Visible = false
EQUIP_BUTTON.Text = “UNEQUIP”
EQUIP_BUTTON.BackgroundColor3 = Color3.new(255/255, 0/255, 0/255) --set color of button
elseif player.ownedtools:FindFirstChild(thecamera.Toolname.Value) and equippedTool ~= thecamera.Toolname.Value then --if the above doesn’t happen then if it is in the player’s ownedtools and the equipped tool is not the same as the one in ownedtools then
EQUIP_BUTTON.Visible = true --give them equip button
BUY_BUTTON.Visible = false --take away buy button
EQUIP_BUTTON.Text = “EQUIP” --make the text equip so they can equip
EQUIP_BUTTON.BackgroundColor3 = Color3.new(56/255, 202/255, 255/255) --change color
else --and if neither of those happen, the item must not be owned
EQUIP_BUTTON.Visible = false --if it isnt owned they cant equip it
BUY_BUTTON.Visible = true --so they get the buy button
BUY_BUTTON.Text = “BUY - $”…thecamera.Cost.Value --make sure the buy button has the right text
–RESETING THE EQUIP BUTTON
EQUIP_BUTTON.Text = “EQUIP” --set the text, shouldn’t be a problem if the item is already equipped because of above statements.
EQUIP_BUTTON.BackgroundColor3 = Color3.new(56/255, 202/255, 255/255) --set the color
end
end

local function Tween_Camera()
is_tweening = true
local target_CF = cameras[“Camera”…current_camera].CFrame

local tween = tween_service:Create(player_camera, tween_info, {CFrame = target_CF})
local listener

listener = tween.Completed:Connect(function()
is_tweening = false
listener:Disconnect()
end)

tween:Play()
end

script.Parent.LeftArrow.Activated:Connect(function()
if not is_tweening then
current_camera = (current_camera + 1 > camera_count and 1) or camera_count + 1
updateButton(cameras:FindFirstChild(“Camera”…current_camera))
Tween_Camera()
end
end)

script.Parent.LeftArrow.Activated:Connect(function()
if not is_tweening then
current_camera = (current_camera - 1 <= 0 and camera_count) or current_camera - 1
updateButton(cameras:FindFirstChild(“Camera”…current_camera))
Tween_Camera()
end
end)

script.Parent.RightArrow.Activated:Connect(function()
if not is_tweening then
current_camera = (current_camera - 1 <= 0 and camera_count) or camera_count - 1
updateButton(cameras:FindFirstChild(“Camera”…current_camera))
Tween_Camera()
end
end)

script.Parent.RightArrow.Activated:Connect(function()
if not is_tweening then
current_camera = (current_camera + 1 > camera_count and 1) or current_camera + 1
updateButton(cameras:FindFirstChild(“Camera”…current_camera))
Tween_Camera()
end
end)

BUY_BUTTON.MouseButton1Click:connect(function()
if not player.ownedtools:FindFirstChild(cameras:FindFirstChild(“Camera”…current_camera).Toolname.Value) then
purchase:FireServer(cameras:FindFirstChild(“Camera”…current_camera).Cost.Value, cameras:FindFirstChild(“Camera”…current_camera).Toolname.Value)
BUY_BUTTON.Visible = false
EQUIP_BUTTON.Visible = true
EQUIP_BUTTON.MouseButton1Click:connect(function()
if EQUIP_BUTTON.Text ~= “UNEQUIP” then
EQUIP_BUTTON.Text = “UNEQUIP”
EQUIP_BUTTON.BackgroundColor3 = Color3.new(255/255, 0/255, 0/255)
equippedTool = cameras:FindFirstChild(“Camera”…current_camera).Toolname.Value
game.ReplicatedStorage.toolEquipper:FireServer(cameras:FindFirstChild(“Camera”…current_camera).Toolname.Value, true)
else
EQUIP_BUTTON.Text = “EQUIP”
EQUIP_BUTTON.BackgroundColor3 = Color3.new(56/255, 202/255, 255/255)
equippedTool = nil
game.ReplicatedStorage.toolEquipper:FireServer(cameras:FindFirstChild(“Camera”…current_camera).Toolname.Value, false)
end
end)
end
end)

SERVER SCRIPT
local purchase = game.ReplicatedStorage:WaitForChild(“PurchaseEvent”)
local equip = game.ReplicatedStorage:WaitForChild(“toolEquipper”)

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

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)

equip.OnServerEvent:connect(function(player, tool, equip)
if equip == true then --Equip is true so lets equip the tool
if player.ownedtools: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()
print(“destroyed a tool in their hands”)
end
end
for i, v in pairs(player.Backpack:GetChildren()) do
if v:IsA(“Tool”) then
v:Destroy()
print(“destroyed a tool in their backpack”)
end
end
–this will equip the players tool from their backpack
local toolCone = player.ownedtools:FindFirstChild(tool):Clone()
toolCone.Parent = player.Backpack
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
for i, v in pairs(player.Backpack:GetChildren()) do
if v:IsA(“Tool”) then
v:Destroy()
end
end
end
end)