Gun shop Messed up

Hello everyone! I’m trying to make a shop for my gun game where you can equip an item when the game starts, for me some of the text doesn’t show up, for example when I buy it it says ‘Bought!’, then it changes to ‘equip’ but, when I press equip it says unequipped, it is suppose to say equipped, any help would be fantastic! (This is similar to Alvin blox’s shop tutorial).

If you want more info reply to me!

Script when buying an item:

buyButton.MouseButton1Click:Connect(function()
local result = game.ReplicatedStorage.PurchaseItem:InvokeServer(selectedItem.Value)
if result == true then
buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
buyButton.Text = “Bought!”
wait(0.5)
buyButton.Text = “Equip”
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
elseif result == “NotEnoughCredits” then
buyButton.BackgroundColor3 = Color3.fromRGB(204,31,31)
buyButton.Text = “Not Enough Credits”
wait(0.5)
buyButton.Text = “Buy”
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
elseif result == “Equipped” then
equippedItem.Value = selectedItem.Value
buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
buyButton.Text = “Equipped!”
wait(0.5)
buyButton.Text = “Unequip”
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
elseif result == “Unequipped” then
equippedItem.Value = “”
buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
buyButton.Text = “Unequipped!”
wait(0.5)
buyButton.Text = “Equip”
buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
end
end)

Server script:

game.ReplicatedStorage:WaitForChild("GetTools").OnServerInvoke = function(player)
	local items = {}
	
	for _, object in pairs(game.ServerStorage:WaitForChild("Items"):GetChildren()) do
		local itemProperties = {object.Name,object.Price.Value}
		table.insert(items,itemProperties)
	end
	
	return items
end

game.ReplicatedStorage:WaitForChild("ItemCheck").OnServerInvoke = function(player,itemName)
	if game.ServerStorage.PlayerData:FindFirstChild(player.Name).Inventory:FindFirstChild(itemName) then
		return true
	else
		return false
	end
end

game.ReplicatedStorage:WaitForChild("PurchaseItem").OnServerInvoke = function(player,itemName)
	local Credits = player.leaderstats.Credits
	local item = game.ServerStorage.Items:FindFirstChild(itemName)
	
	if item then
		--Item exists
		if game.ServerStorage.PlayerData[player.Name].Inventory:FindFirstChild(itemName) then
			if game.ServerStorage.PlayerData[player.Name].Equipped.Value == itemName then
				-- Currency Unequipped
				game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName
				return "Equipped"
			else
				game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
				return "Unequipped"
			end 
		end
		
		if Credits.Value >= item.Price.Value then
			Credits.Value = Credits.Value - item.Price.Value
			
			local itemValue = Instance.new("ObjectValue")
			itemValue.Name = itemName
			itemValue.Parent = game.ServerStorage.PlayerData[player.Name].Inventory
			
			return true
		else
			
			return "NotEnoughCredits"
		end
		
	else
		return "NoItem"
	end
end

Need help asap

Hello, Does It give you any error codes? did you try debugging? It is a bit hard for me to really understand what is the problem here but I will still try to help you as I can :slight_smile:

Equipping Logic:

In your server script, the logic for equipping and unequipping seems to be reversed:

if game.ServerStorage.PlayerData[player.Name].Equipped.Value == itemName then
    game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName
    return "Equipped"
else
    game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
    return "Unequipped"
end 

If the player’s equipped item is the same as the item they’re trying to equip, you’re setting their equipped item to that item again (which is redundant) and returning “Equipped”. Otherwise, you’re unequipping the item. This logic seems backward. You probably want to equip the item if it’s not already equipped and unequip it if it is.

if game.ServerStorage.PlayerData[player.Name].Equipped.Value == itemName then
    game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
    return "Unequipped"
else
    game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName
    return "Equipped"
end 

**In your client script, you’re checking for the result “Equipped” and “Unequipped” from the server. However, in your server script, you’re returning “Equipped” and “Unequipped” only if the player already owns the item. This might be causing confusion.

If the player is buying an item for the first time, the server script will return true, and the client script will display “Bought!” and then “Equip”. But if the player already owns the item, the server script will return either “Equipped” or “Unequipped”, and the client script will display “Equipped!” or “Unequipped!” respectively.**

No error codes all at, don’t know what is the problem

Double posting your thread won’t get you an answer faster. You should’ve just waited for help instead of creating 2 of the same posts.
EDIT: Just saw you created 3 of the same threads…

Anyways, try this code, it might work.
Let me know if it doesn’t! :+1:

buyButton.MouseButton1Click:Connect(function()
    local result = game.ReplicatedStorage.PurchaseItem:InvokeServer(selectedItem.Value)
    if result == true then
        buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
        buyButton.Text = "Bought!"
        wait(0.5)
        if equippedItem.Value == selectedItem.Value then
            buyButton.Text = "Equipped"
        else
            buyButton.Text = "Equip"
        end
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    elseif result == "NotEnoughCredits" then
        buyButton.BackgroundColor3 = Color3.fromRGB(204,31,31)
        buyButton.Text = "Not Enough Credits"
        wait(0.5)
        buyButton.Text = "Buy"
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    elseif result == "Equipped" then
        equippedItem.Value = selectedItem.Value
        buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
        buyButton.Text = "Equipped!"
        wait(0.5)
        buyButton.Text = "Unequip"
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    elseif result == "Unequipped" then
        equippedItem.Value = ""
        buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
        buyButton.Text = "Unequipped!"
        wait(0.5)
        buyButton.Text = "Equip"
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    end
end)

Yeah sry about that, needed something to work asap

Edit: Does the same thing, would you like more info about the scripts, problem etc.

Still isn’t a valid reason to post multiple threads about the same issue.
But yes, more information would be useful.

Vid of the problem

robloxapp-20231006-1758490.wmv (630.4 KB)

Whole script with the Buybutton script


-- Core UI localscript

local availableTools = game.ReplicatedStorage:WaitForChild("GetTools"):InvokeServer()
local mainFrame = script.Parent:WaitForChild("MainFrame")
local safeArea = mainFrame:WaitForChild("SafeArea")
local itemInformation = safeArea:WaitForChild("ItemInformation")
local infoFrame = itemInformation.InfoFrame
local selectedItem = itemInformation.SelectedItem
local equippedItem = itemInformation.EquippedItem
local numberOfItems = #availableTools

local itemFrame = safeArea.ItemFrame
local shopButton = script.Parent:WaitForChild("ShopButton")
local buyButton = infoFrame.BuyButton
local equippedItemViewport = script.Parent:WaitForChild("EquippedItemViewport")
local itemViewport = itemInformation.ItemViewport



shopButton.MouseButton1Click:Connect(function()
	mainFrame.Visible = not mainFrame.Visible
end)

local PADDING_X = 0.02
local DROPDOWN_Y = 0.2
local DROPDOWN_X = 0.25

local item1 = itemFrame:WaitForChild("Item1")

local box
local numRows = 1

for i = 1,numberOfItems,1 do
	
	if i == 1 then
		box = item1
	else
		
		box = item1:Clone()
		box.Name = "Item"..i
		box.Parent = itemFrame
		
		if (i-1) / (4*numRows) == 1 then
			-- New row
			numRows = numRows + 1
			box.Position = UDim2.new(PADDING_X,0,box.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_Y* (numRows - 1))
		else
			-- Add to the X only
			box.Position = itemFrame["Item"..(i-1)].Position + UDim2.new(DROPDOWN_X,0,0,0)
		end
	end
	
	
	box.MouseButton1Click:Connect(function()
		for _, v in pairs(itemViewport:GetChildren()) do
			if not v:IsA("Frame") then
				v:Destroy()
			end
		end
		
		local itemViewportCam = Instance.new("Camera")
		itemViewportCam.Parent = itemViewport
		
		local handle = game.ReplicatedStorage:WaitForChild("ToolModules"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
		handle.Parent = itemViewport
		
		itemViewport.CurrentCamera = itemViewportCam
		itemViewportCam.CFrame = handle.CameraCFrame.Value
		
		local owned = game.ReplicatedStorage.ItemCheck:InvokeServer(availableTools[i][1])
		
		if equippedItem.Value == availableTools[i][1] then
			infoFrame.Credits.Text = "Owned"
			infoFrame.BuyButton.Text = "Unequip"
		elseif owned == true then
			infoFrame.Credits.Text = "Owned"
			infoFrame.BuyButton.Text = "Equip"
		else
			infoFrame.BuyButton.Text = "Buy"
			infoFrame.Credits.Text = "$"..availableTools[i][2]
		end
		
		infoFrame.ItemName.Text = availableTools[i][1]
		selectedItem.Value =availableTools[i][1]
		
		for _, v in pairs(itemFrame:GetChildren()) do
			if v:IsA("ImageButton") then
				v.BorderSizePixel = 0
			end
		end
		
		itemFrame["Item"..i].BorderSizePixel = 2
		
	end)
	
	local fakeCam = Instance.new("Camera")
	fakeCam.Parent = box.VPF
	local handle = game.ReplicatedStorage:WaitForChild("ToolModules"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
	handle.Parent = box.VPF
	box.VPF.CurrentCamera = fakeCam
	fakeCam.CFrame = handle.CameraCFrame.Value
	itemFrame["Item"..i].ItemName.Text = availableTools[i][1]
	
end

buyButton.MouseButton1Click:Connect(function()
    local result = game.ReplicatedStorage.PurchaseItem:InvokeServer(selectedItem.Value)
    if result == true then
        buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
        buyButton.Text = "Bought!"
        wait(0.5)
        if equippedItem.Value == selectedItem.Value then
            buyButton.Text = "Equipped"
        else
            buyButton.Text = "Equip"
        end
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    elseif result == "NotEnoughCredits" then
        buyButton.BackgroundColor3 = Color3.fromRGB(204,31,31)
        buyButton.Text = "Not Enough Credits"
        wait(0.5)
        buyButton.Text = "Buy"
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    elseif result == "Equipped" then
        equippedItem.Value = selectedItem.Value
        buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
        buyButton.Text = "Equipped!"
        wait(0.5)
        buyButton.Text = "Unequip"
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    elseif result == "Unequipped" then
        equippedItem.Value = ""
        buyButton.BackgroundColor3 = Color3.fromRGB(42,149,42)
        buyButton.Text = "Unequipped!"
        wait(0.5)
        buyButton.Text = "Equip"
        buyButton.BackgroundColor3 = Color3.fromRGB(55,193,55)
    end
end)



if equippedItem.Value ~= "" then
	local fakeCam = Instance.new("Camera")
	fakeCam.Parent = equippedItem
	local handle = game.ReplicatedStorage:WaitForChild("ToolModules"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
	handle.Parent = equippedItemViewport
	equippedItemViewport.CurrentCamera = fakeCam
	fakeCam.CFrame = handle.CameraCFrame.Value
end

equippedItem:GetPropertyChangedSignal("Value"):Connect(function()
	if equippedItem.Value ~= "" then

		for _, v in pairs(equippedItemViewport:GetChildren()) do
			if not v:IsA("Folder") then
				v:Destroy()
			end
		end

		local fakeCam = Instance.new("Camera")
		fakeCam.Parent = equippedItem
		local handle = game.ReplicatedStorage:WaitForChild("ToolModules"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
		handle.Parent = equippedItemViewport
		equippedItemViewport.CurrentCamera = fakeCam
		fakeCam.CFrame = handle.CameraCFrame.Value
	else
		for _, v in pairs(equippedItemViewport:GetChildren()) do
			if not v:IsA("Folder") then
				v:Destroy()
			end
		end
	end
end)

Could you edit the serverscript code with some more debugging prints?

local Credits = player.leaderstats.Credits
local item = game.ServerStorage.Items:FindFirstChild(itemName)

if item then
    print("[0] Item exists")
	--Item exists
	if game.ServerStorage.PlayerData[player.Name].Inventory:FindFirstChild(itemName) then
        print("[0] Inventory Item found")
		if game.ServerStorage.PlayerData[player.Name].Equipped.Value == itemName then
            game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
            print("[0] Unequipping")
            return "Unequipped"
        else
            game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName
            print("[0] Equipping")
            return "Equipped"
        end
    else
        print("[0] Cant find item in plr data")
	end
    
	
	if Credits.Value >= item.Price.Value then
		Credits.Value = Credits.Value - item.Price.Value
		
		local itemValue = Instance.new("ObjectValue")
		itemValue.Name = itemName
		itemValue.Parent = game.ServerStorage.PlayerData[player.Name].Inventory
		
		return true
	else
		
		return "NotEnoughCredits"
	end
	
else
	return "NoItem"
end

Wow it worked thank you so much! Just needed debugging prints, now I know in the future!

No problem at all man. You can remove the prints if you want but i’m glad it worked.
Don’t post 3 times again haha, its not needed. Goodluck with your game :+1:

Yeah, Learned something new today, don’t spam threads!