I recently attempted to script collectable objects locally and make their changes reset when the player dies.
I did this by using a local script in a ScreenGui with ResetOnSpawn enabled, and got the orbs (which have the same name) using In Pairs loop.
local Orbs
for i, v in pairs(workspace:GetChildren()) do --this loop gets the orbs from the workspace and specifies it as Orbs
if v.Name == "Orb" then
Orbs = v
end
end
Orbs.Material = Enum.Material.ForceField
local NavyBlueValue = script["Navy Blue"].Value
local NavyBlueCollected = script["Navy Blue"].Collected
if workspace.OrbPlatform.Transparency ~= 1 then
local Info = TweenInfo.new(1)
local Tween = game:GetService("TweenService"):Create(workspace.OrbPlatform,Info,{Transparency=1})
Tween:Play()
Tween.Completed:Connect(function()
workspace.OrbPlatform.CanCollide = true
workspace.OrbPlatform.CanTouch = true
end)
end
print(1)
Orbs.Collected.Value = false
NavyBlueCollected.Value = 0
Orbs.Touched:Connect(function(hit) --This is where it stopped after trying to collect the second orb
print(2)
print(Orbs)
local Debounce = true
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Player ~= nil and Humanoid ~= nil and Player == game.Players.LocalPlayer then
print(3)
if Humanoid.Health ~= 0 then
print(4)
if Player.TeamColor ~= BrickColor.new("Navy blue") then return end
if Orbs.Collected.Value == false then
print(5)
NavyBlueCollected.Changed:Connect(function()
print(6)
if NavyBlueCollected.Value == NavyBlueValue then
print(7)
if workspace.OrbPlatform.Transparency ~= 0 then
local Info = TweenInfo.new(1)
local Tween = game:GetService("TweenService"):Create(workspace.OrbPlatform,Info,{Transparency=0})
Tween:Play()
Tween.Completed:Connect(function()
workspace.OrbPlatform.CanCollide = true
workspace.OrbPlatform.CanTouch = true
end)
end
else
print(0)
end
end)
NavyBlueCollected.Value = NavyBlueCollected.Value + 1
Orbs.Collected.Value = true
Orbs.Material = Enum.Material.ForceField
end
end
end
end)
Any help is appreciated!
EDIT: I found out it only detects one orb and I don’t know what’s wrong there, because when I collect it first it still doesn’t work. Also ignore debounce for now.