How do I make it so the player can collect an item once

This is my code how would I make it that a player can only collect this once
(Bad code ik I’m new)

script.Parent.Triggered:Connect(function(plr)
plr.leaderstats.Pizzas.Value = plr.leaderstats.Pizzas.Value +1
local hum = plr.Character:WaitForChild(“Humanoid”)
local loadanim = hum.Animator:LoadAnimation(script.Parent.Animation)
script.Parent:Destroy()
loadanim:Play()
task.wait(5)
loadanim:Stop()

end)

easiest method is to make a Boolean Value on Script.Parent, and set it to true after its collected. then, when the player attempts to collect it again, just check if the Boolean Value is true or not, and if its true, just end the script.

Since this appears to be a server script because you’re manipulating leaderstats I assume the best method would be a table:

local plrCollected = {}

script.Parent.Triggered:Connect(function(plr)
	if not table.find(plrCollected, plr.Name) then
		table.insert(plrCollected, plr.Name)
		plr.leaderstats.Pizzas.Value += 1

		local hum = plr.Character:FindFirstChild("Humanoid")
		local anim = hum.Animator:LoadAnimation(script.Parent.Animation)

		script.Parent:Destroy()

		anim:Play()
		task.wait(5)
		anim:Stop()
	end
end)

To let a player collect it again simply remove them from the table:

table.remove(plrCollected, table.find(plrCollected, plr.Name))

To let everyone collect it again, clear the table:

table.clear(plrCollected)

Hope this helps! :slight_smile:

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