Saving whether or not a player has an item inside a script?

local playerGuns = {
	'Starter' == true;
	'Shotgun' == false;
	'Rifle' == false;
	'Burst' == false;
	'Heavy' == false;
	'Sniper' == false;
	'Rocket' == false;
}

    for _, v in pairs(playerGuns) do
    	checkItem:FireServer('Gun', v)
    end

    checkItem.OnClientEvent:Connect(function(hasItem, item)
    	if hasItem then
    		table.insert(playerGuns, item)
    	end
    end)

    selectedItem:GetPropertyChangedSignal('Value'):Connect(function()
    local hasGun = false

    `-- somehow go through table and turn hasGun to true if player has the item with the same name as say a value? selectedItem.Value`
    	if hasGun then
    		print('Has the gun unlocked')
    	else
    		print('Hasnt got the gun unlocked')
    	end	
    end)

I’ve left out all the unnecessary stuff. The remote event works, it fires whenever the local script loads (when player joins, respawns, etc.) When it fires back to the client, it inserts the select ‘item’ into the playerGuns table. I’m having trouble however checking to see if said player has said gun. I’m not sure how I’d loop through the playerGuns table to make sure that they have the same gun as what the selectedItem.Value would be?

could you explain more about your issue?
feel free to add me on discord DutchDeveloper™#7064 maybe there we can talk

What’s the point of that playerGuns table? That’s just going to evaluate as {false, false, false, false, false, false, false}

I have no clue what I’m doing now tbh :confused:

You could use a dictionary instead of a table. In my opinion they are better to use instead of arrays for things like this.
Ex:

local playerGuns = {
	["Starter"] = true;
	["Shotgun"] = false;
	["Rifle"] = false;
	["Burst"] = false;
	["Heavy"] = false;
	["Sniper"] = false;
	["Rocket"] = false;
}

That’s what I had?

Oh wait no sorry, i see now, it’s almost midnight for me and my minds gone to kaplutz

Definitely use this way. Then when they have the gun continue what you’re doing by setting it to true.
Then use the instance value’s value to detect whether its true or not.

if playerGuns[selectedItem.Value] == true then

2 Likes

See with a dictionary as opposed to checking each individual object in the array you can just do this.

local Example = {
	["lol"] = true,
	["lel"] = false
}

if Example["lol"] then
	print("lol") --Prints "lol" to output
else
	print("Nope") --Will not Print
end

if Example["lel"] then
	print("lel") --Will not Print
else
	print("Nope") --Will print "nope" to the output
end