So I’ve made an orb collection system that works on the client side (idc about remote protection rn).
How the script works is it indexes a list of orbs in a folder contained in workspace, and places each object it finds in a table on the client side.
Then there is a function that runs upon an orb being touched, which checks if the orb touched is found in the table, and if so, remove it from the table. Now if there is nothing in the table after this, the script will print(“All orbs collected!”).
There is a server script in SSService that loops over every orb in the Collectibles folder, and sets up an event system where it checks if one is touched, and if they are then it checks if the player who touched it is in a table full of players who touched the orb, if it isn’t it continues and fires a remote with the player argument and the orb argument.
Now when i run these scripts, i touch an orb and it correctly detects it not being in the indexed table of orbs and removes it from there. But then when i touch a different orb, nothing happens. And this is not just to do with a specific orb being out of place, but this happens with any orb i touch first, then try to touch another one secondly. The table is correct, as i made it print how much objects are contained inside.
Here are the scripts i was talking about:
LocalScript found in ReplicatedFirst:
local Collectibles = workspace:WaitForChild("Map"):WaitForChild("Collectibles")
local CollectEvent = game:GetService("ReplicatedStorage").Events.Collect
local OrbsLeft = {}
repeat wait() until Collectibles:GetChildren()[6]
print(Collectibles.Parent, Collectibles:GetChildren())
for i, v in pairs(Collectibles:GetChildren()) do
table.insert(OrbsLeft, v)
print("Found orb")
end
function OrbTouched(orb)
print("Orb touched!")
if table.find(OrbsLeft, orb) ~= nil then
print("Found orb in table, removing...")
local index = table.find(OrbsLeft, orb)
table.remove(OrbsLeft, index)
orb:Destroy()
end
if #OrbsLeft == 0 then
print("All orbs collected!")
end
end
CollectEvent.OnClientEvent:Connect(function(orb)
OrbTouched(orb)
end)
Server script found in ServerScriptService:
local CollectEvent = game:GetService("ReplicatedStorage").Events.Collect
local Collected = {}
local Collectibles = game.Workspace.Map:WaitForChild("Collectibles")
for i, orb in pairs(Collectibles:GetChildren()) do
orb.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player and table.find(Collected, Player) == nil then
table.insert(Collected, Player)
CollectEvent:FireClient(Player, orb)
end
end
end)
task.wait()
end
i have been looking across the forum and asking chatgpt, but i cannot find a conclusion or a fix for this. any help is appreciated!!!