Hello! For my game, I am implementing a system commonly found in other Roblox games where there are collectibles around the map. I want each player to individually be able to collect the item ONCE and no more than that.
The above statement I have achieved when dealing with a single player. The problem is that nobody else is able to collect the item after the one player has obtained it. Video below. (Watch the point counter go from 500 to 550)
I believe the system that I have begun to put in place is on the right track but I am missing a thing or two. Whenever the player interacts with the collectibles hitbox, a tag gets added to their player that checks that they have collected the item and if they have said tag, the item cannot be picked up anymore and this works great… until the other player isn’t able to get the tag.
What check do I need to add? Or is there an entirely better way to do it? I’ll leave the entire collectible script below.
local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")
local collectiblesFolder = game.Workspace.Collectibles
local collectiblesTable = collectiblesFolder:GetChildren()
local function gems()
for _, gem in pairs (collectiblesTable) do
local hitbox = gem:WaitForChild("Hitbox")
local primaryPart = gem:WaitForChild("CenterPart")
--Styling information. Not apart of the problem.
local gemTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true, 0)
local gemTweenProperties = {
["CFrame"] = CFrame.new(primaryPart.Position) + Vector3.new(0,5,0)
}
local gemTween = tweenService:Create(primaryPart, gemTweenInfo, gemTweenProperties)
gemTween:Play()
----------------------------------------------------
local collectedTag = Instance.new("BoolValue")
collectedTag.Name = "Collected "..gem.Name
collectedTag.Value = false
hitbox.Touched:Connect(function(otherPart)
local player = players:GetPlayerFromCharacter(otherPart.Parent)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if player and humanoid then
collectedTag.Parent = player
if collectedTag.Value == false then
collectedTag.Value = true
player.leaderstats.Points.Value += 50
print("touched")
end
end
end)
end
end
gems()