Check for tagged items

For my basketball game, I use collection service to keep track of all the balls. How can I make sure a player does not have an item that is tagged “Basketball” I need to use tags because my balls are going to have different names. Thanks.

1 Like

It depends on how your system currently runs. For most cases a simple ‘if CollectionService:HasTag(ball, “tag”) then’ would work.

“tag” being whatever you called your tag.

Yeah, but I don’t actually have a variable for the ball. I tried using a function, and it was returning false for whether or not I had a ball, but when putting it in the if statement, it did not work.

I’m not really sure what you mean. I’m guessing a certain number of balls exist in the game, so if someone tries to pick up the wrong type of ball you could just use an if statement to prevent them from doing so. Otherwise I don’t really get how the player would get the ball you don’t want them to have in the first place.

It’s quite hard to know what you mean if you don’t provide any code.

I’m not great at explaining things but I’ll do my best. I have 10 balls in my game (it’s a private shootaround) and if you already have a ball I want you to be unable to pick up another one. The balls have different names, so I can’t just do a name check. I will provide some code give me a second.

Then just use a variable, and if that variable is true then you can’t pick up another ball. Like ‘if ballheld == true then’.

local function FindBallInv(char)
    local balls = 0
    for i,v in pairs(char:GetChildren()) do
	    if CS:HasTag(v,"Basketball") then
		    balls = balls + 1
	    end
    end
    if balls > 0 then
	    return true
    else
	    return false
    end
 end

Basketball.Touched:Connect(function(touch)
    if touch.Parent:FindFirstChild("Humanoid") and not Basketball.Parent:FindFirstChild("Humanoid") and canBlock == false then
	    local char = touch.Parent
	    if debounces[char] == false and FindBallInv(char) == false then
		    Basketball.Parent = char
	    end
    end
end

So what are you trying to achieve that isn’t working. You want the person to only have 1 ball at a time, right?

You can try using CollectionService | Roblox Creator Documentation, and verifying if one of the parts is a descendant of the character

2 Likes

Yes and my function is returning false but it still will not work when put into the if statement.

Alright I will try that. Thank you.

You could do what @Jxl_s suggested, but I feel like you’re over complicating your script. You could achieve the same thing by putting in some kind of debounce, which is only changed if the person removes the ball from their inventory. If you have multiple scripts and so cannot use a local variable you could use a boolval or attribute.

1 Like

oh yeah, now that I think about it, I could just set the debounce for the character to true while they have the ball instead of when the drop it. I really am overcomplicating it.

1 Like