Datastore shop script won't unequip items in game

So, In a game I’m developing, where I use DataStores to save progress if a player leaves or bought or equipped/unequipped something. However, for some reason, anything with an Item status of “Equipped” won’t do anything on the server end.

What is supposed to happen is that if a player selected to unequip an item (The Status of the item being Equipped before it is Unequipped), it would unequip it and would allow them to have the choice to not use gear items in a round, or emotes, or a morph.

The prints don’t even come up either, or the warn, it just skips right to where it returns the PlayerData table.

However, I noticed that by putting an if statement before the huge if then elseif statements where it processes the item status, it would go through it, and run anything there (I put a comment where I did that). However, it might break the other if statements and not run them.

This is the function used to process Client-sided purchases in a shop GUI:

ReplicatedStorage.Events.Functions.InteractItem.OnServerInvoke = function(Player, ItemName, ItemType)
	print(Player, ItemName, ItemType)
	local ShopItem = nil

	if ItemType == "Killer" then
		ShopItem = ShopMod.Killers[ItemName]
	elseif ItemType == "Gear" then
		ShopItem = ShopMod.Gears[ItemName]
	elseif ItemType == "Emote" then
		ShopItem = ShopMod.Emotes[ItemName]
	elseif ItemType == "Morph" then
		ShopItem = ShopMod.Morphs[ItemName]
	end

	local PlayerData = Data[Player.UserId]

	if ShopItem and PlayerData then
		local Status = GetItemStatus(Player, ItemName, ItemType)
		print("Status is", Status)
		
		-- This is where I tested if it even checked for the Status, and it would print, if it was placed here.
		-- if Status == "Equipped" then
			--print("This item is Equipped in the player's inventory")
		--end
		
		if Status == "For Sale" then
			print("Processing Sale...")
			if ShopItem.Price <= PlayerData.Money then
				PlayerData.Money -= ShopItem.Price
				if ItemType == "Killer" then
					table.insert(PlayerData.OwnedKillers, ItemName)
				elseif ItemType == "Gear" then
					table.insert(PlayerData.OwnedGears, ItemName)
				elseif ItemType == "Emote" then
					table.insert(PlayerData.OwnedEmotes, ItemName)
				elseif ItemType == "Morph" then
					table.insert(PlayerData.OwnedMorphs, ItemName)
				end
			else
				return false
			end
			
		elseif Status == "Owned" then
			print("Checking if equipped...")
			if ItemType == "Killer" then
				print("Killer")
				table.insert(PlayerData.EquippedKiller, ItemName)
				if #PlayerData.EquippedKiller > 1 then
					table.remove(PlayerData.EquippedKiller, 1)
				end
			elseif ItemType == "Gear" then
				table.insert(PlayerData.GearLoadout, ItemName)
				if #PlayerData.GearLoadout > MAX_GEAR_LOADOUT then
					table.remove(PlayerData.GearLoadout, 1)
				end
			elseif ItemType == "Emote" then
				table.insert(PlayerData.EquippedEmotes, ItemName)
				if #PlayerData.EquippedEmotes > MAX_EMOTES then
					table.remove(PlayerData.EquippedEmotes, 1)
				end
			elseif ItemType == "Morph" then
				table.insert(PlayerData.EquippedMorph, ItemName)
				if #PlayerData.EquippedMorph > 1 then
					table.remove(PlayerData.EquippedMorph, 1)
				end
				
			elseif Status == "Equipped" then -- This is where it wouldn't work
				print("Checking to unequip...")
				if ItemType == "Killer" then
					if #PlayerData.EquippedKiller == 1 then
						return false
					end
				elseif ItemType == "Gear" then
					local GearToRemove = table.find(PlayerData.GearLoadout, ItemName)
					--if GearToRemove then
						table.remove(PlayerData.GearLoadout, GearToRemove)
					--end
					
				elseif ItemType == "Emote" then
				
					local EmoteToRemove = table.find(PlayerData.EquippedEmotes, ItemName)
					if EmoteToRemove then
						table.remove(PlayerData.EquippedEmotes, EmoteToRemove)
					end
					
				elseif ItemType == "Morph" then
					table.remove(PlayerData.EquippedMorph, 1)
				end
				
			end
		end
		return PlayerData

	else
		warn("! No Item / PlayerData Detected !")
	end
	
	return false
end

Now, given it worked before for me in a different game, albeit with less item types to go through, I am thoroughly confused why it doesn’t here, even though it’s basically the same script code, just modified. So, if anybody would just tell me where I went wrong, then I’d be eternally grateful.

Thanks.

1 Like

Pretty sure there is a formatting issue with one of the if statements. I will investigate.

Try this:

ReplicatedStorage.Events.Functions.InteractItem.OnServerInvoke = function(Player, ItemName, ItemType)
	print(Player, ItemName, ItemType)
	local ShopItem = nil

	if ItemType == "Killer" then
		ShopItem = ShopMod.Killers[ItemName]
	elseif ItemType == "Gear" then
		ShopItem = ShopMod.Gears[ItemName]
	elseif ItemType == "Emote" then
		ShopItem = ShopMod.Emotes[ItemName]
	elseif ItemType == "Morph" then
		ShopItem = ShopMod.Morphs[ItemName]
	end

	local PlayerData = Data[Player.UserId]

	if ShopItem and PlayerData then
		local Status = GetItemStatus(Player, ItemName, ItemType)
		print("Status is", Status)

		-- This is where I tested if it even checked for the Status, and it would print, if it was placed here.
		-- if Status == "Equipped" then
		--print("This item is Equipped in the player's inventory")
		--end

		if Status == "For Sale" then
			print("Processing Sale...")
			if ShopItem.Price <= PlayerData.Money then
				PlayerData.Money -= ShopItem.Price
				if ItemType == "Killer" then
					table.insert(PlayerData.OwnedKillers, ItemName)
				elseif ItemType == "Gear" then
					table.insert(PlayerData.OwnedGears, ItemName)
				elseif ItemType == "Emote" then
					table.insert(PlayerData.OwnedEmotes, ItemName)
				elseif ItemType == "Morph" then
					table.insert(PlayerData.OwnedMorphs, ItemName)
				end
			else
				return false
			end
			
		elseif Status == "Owned" then
			print("Checking if equipped...")
			if ItemType == "Killer" then
				print("Killer")
				table.insert(PlayerData.EquippedKiller, ItemName)
				if #PlayerData.EquippedKiller > 1 then
					table.remove(PlayerData.EquippedKiller, 1)
				end
			elseif ItemType == "Gear" then
				table.insert(PlayerData.GearLoadout, ItemName)
				if #PlayerData.GearLoadout > MAX_GEAR_LOADOUT then
					table.remove(PlayerData.GearLoadout, 1)
				end
			elseif ItemType == "Emote" then
				table.insert(PlayerData.EquippedEmotes, ItemName)
				if #PlayerData.EquippedEmotes > MAX_EMOTES then
					table.remove(PlayerData.EquippedEmotes, 1)
				end
			elseif ItemType == "Morph" then
				table.insert(PlayerData.EquippedMorph, ItemName)
				if #PlayerData.EquippedMorph > 1 then
					table.remove(PlayerData.EquippedMorph, 1)
				end
			end
			elseif Status == "Equipped" then
				print("Checking to unequip...")
				if ItemType == "Killer" then
					if #PlayerData.EquippedKiller == 1 then
						return false
					end
				elseif ItemType == "Gear" then
					local GearToRemove = table.find(PlayerData.GearLoadout, ItemName)
					--if GearToRemove then
					table.remove(PlayerData.GearLoadout, GearToRemove)
					--end

				elseif ItemType == "Emote" then

					local EmoteToRemove = table.find(PlayerData.EquippedEmotes, ItemName)
					if EmoteToRemove then
						table.remove(PlayerData.EquippedEmotes, EmoteToRemove)
					end

				elseif ItemType == "Morph" then
					table.remove(PlayerData.EquippedMorph, 1)
				end
			end
			
			
		
		
		return PlayerData

	else
		warn("! No Item / PlayerData Detected !")
	end

	return false
end
1 Like
elseif Status == "Owned" then
    ...
    elseif Status == "Equipped" then --never runs if this isn't owned
        ...
if Status == "For Sale" then
    -- handle sale
elseif Status == "Owned" then
    -- handle equip
elseif Status == "Equipped" then
    -- handle unequip
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.