Equip/Unequip messed up

Making 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!

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)

2 Likes

You could check if the player has the gun in the player’s backpack

2 Likes

and how would i do that, give me an example.

1 Like

Probably something like this I would guess

local player = game.Players.LocalPlayer
local item -- get the item Path or customize it

elseif result == "Equipped" and player.Backpack.item then

elseif result == "Unequipped" and not player.Backpack.item then

end

1 Like

didn’t really work, the button did not work at all, anything else?

1 Like

Is it possible for you to provide a video? Since I don’t really see a mistake in the script.

1 Like

robloxapp-20231006-0847371.wmv (692.9 KB)
When I press equip it says unequipped then back to equip instead of equipped to unequip, the item also does not pop up on the item box on the bottom left corner

1 Like

I am not really sure now. I’ll investigate it in a bit. But maybe try using return in the elseifs.

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)
		return
	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)
		return
	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)
		return
	end
1 Like

does the same thing, thanks for having time to help me tho, keep going

1 Like

Could you show the result being Invoked? I am pretty sure it has to do who receives the invoke.

1 Like

like show the function in replicated storage?

1 Like

Well you are sending the data to a server script (since you used :InvokeServer()) else I am just wrong lol

1 Like

ahh yes in server handler this is the part:

game.ReplicatedStorage:WaitForChild(“PurchaseItem”).OnServerInvoke = function(player,itemName)
local Credits = player.leaderstats.Credits
local item = game.ServerStorage.Items:FindFirstChild(itemName)

1 Like

oh. hmmmm I will try to look into this further while you can try around.

1 Like

Ok last information I need: what type of Values are selectedItem and equippedItem?

1 Like

SelectedItem is a remote function in ReplicatedStorage and EquippedItem is a string value in my shop ui

1 Like

my last option is to check whether both of them are the same. I am sorry if this won’t help :confused:

elseif result == "Equipped" then
		equippedItem.Value = selectedItem.Value
		buyButton.BackgroundColor3 = Color3.fromRGB(42, 149, 42)
		buyButton.Text = "Equipped!"
		wait(0.5)
		if equippedItem.Value == selectedItem.Value or result then
			buyButton.Text = "Unequip"
		else
			buyButton.Text = "Equip"
		end
		buyButton.BackgroundColor3 = Color3.fromRGB(55, 193, 55)
1 Like

didn’t work but thanks for coming and helping me!

2 Likes

Since it’s a button, the code could be firing twice. Try adding a debounce to your code.

local debounce = false
buyButton.MouseButton1Click:Connect(function()
if debounce == false then
debounce = true
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
task.wait(0.5)
debounce = false
      end
end)

After what @yoshicoolTV has tried, that’s the only thing I can think of.