Help a new Coder find his Error

So im fairly new to Lua and to coding in general so please forgive my stupidity.
Anyways my plan was if someone presses a the given key than the server should take a Veste away from ur inventory and after than give u an animationen. This works just fine the problem is it still give u the animation and the armor even if u dont have a veste anymore and thats the point where i need help.

client.lua (1.0 KB)
server.lua (490 Bytes)

So add in a check if the player “has” the item.

yeah but how do i do that

I believe a simple if then statement should work here… Basically think if the player has the item required, then.

yeah ok but what i wanted to know is what to put after if and befor then

Oh… so you want someone to write the rest of the code for you rather then learning how to do it yourself?

/me exits stage left…

1 Like

me remembers why i never used forums

You need to store the player inventory in a list.
then you loop through and check if that list contains the item already.

I could be wrong anyone correct me if i am, im a C# coder lol

-- Client side implementation

local inventory = {'item1', 'item5', 'item 9'}

function hasItem(itemName)
	for i, name in ipairs(names) do
		if name == item then
			return true
		end
	end
	return false
end

if not hasItem("item5") then
	table.insert(inventory, "item5")
end

If you want it to be server side

local playerInventories = {}

function createPlayer(playerHandle)
	local player = {}
	player.Handle = playerHandle
	player.Inventory = {}
	return player
end

function createItem(itemName)
	local item = {}
	item.name = itemName
	return item
end

function getPlayerInventory(playerHandle)
	for i, player in ipairs(playerInventories) do
		if player.Handle == playerHandle then
			return player.Inventory
		end
	end
end

function addItemToPlayerInventory(playerHandle, item)
	table.insert(getPlayerInventory(playerHandle), item)
end

function hasPlayerItemInInventory(playerHandle, itemName)
	for i, item in ipairs(getPlayerInventory(playerHandle)) do
		if item.name == itemName then
			return true
		end
	end
	return false
end

...onPlayerConnecting
	createPlayer(src)

...onPlayerPickupItem
	local item = createItem("the item name")
	if not hasPlayerItemInInventory(src, item) then
		addItemToPlayerInventory(src, item)
	end

thx mate u are the best

1 Like

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