My script only works half the time. Im making a Pizza Tower fangame and im trying to make metal blocks you can break, but they only break half the time. How do I fix this? Heres the script:
local Players = game:GetService("Players")
local PanicEvent = game.ReplicatedStorage:WaitForChild("Panic")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- Iterate over all parts in the workspace
for _, part in game.Workspace:GetChildren() do
if part:IsA("BasePart") then
part.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if part.Name == "EscapeTrigger" or part.Name == "MetalBlock" then
if humanoid then
if part.Name == "EscapeTrigger" then
PanicEvent:FireServer()
end
part:Destroy()
end
end
end)
end
end
Well, they might’ve not fully loaded into the game yet upon joining.
Since you’re immediately iterating over the parts in workspace, only the parts that have loaded will be counted in, so that might be the cause.
Unrelated but this line is unnecessary since you already checked that.
my code below didn’t format perfectly, but you should get the overall idea.
this might work for client side:
'''
local Players = game:GetService("Players")
local PanicEvent = game.ReplicatedStorage:WaitForChild("Panic")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local function Panic()
-- Iterate over all parts in the workspace
for _, part in game.Workspace:GetChildren() do
if part:IsA("BasePart") then
part.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if part.Name == "EscapeTrigger" or part.Name == "MetalBlock" then
if humanoid then
if part.Name == "EscapeTrigger" then
PanicEvent:FireServer()
end
part:Destroy()
end
end
end)
end
end
end
game.Workspace.ChildAdded:Connect(Panic)
Panic()
'''
:
if this were changed to a server-side script, you could use Collection Service with Tag Editor:
‘’’
–Use with Tag Window:
local CollectionService = game:GetService("CollectionService")
local PanicEvent = game.ReplicatedStorage:WaitForChild("Panic")
function makePanicPart(part)
local data = {}
data.touchedConn = part.Touched:Connect(function(part)
if part and part.Parent and part.Parent:FindFirstChild("Humanoid") then
local character = part.Parent
local player = game.Players:GetPlayerFromCharacter(character)
PanicEvent:FireClient(player) --or whatever logic is needed
part:Destroy() --may need to have the part Destroy() on the client through the above remote event
end
end)
return data
end
function undoPanicPart(data)
data.touchedConn:Disconnect()
end
------------------------------------------------------------
local PanicParts = {}
local addedSignal = CollectionService:GetInstanceAddedSignal("PanicPart")
local removedSignal = CollectionService:GetInstanceRemovedSignal("PanicPart")
local function onAdded(part)
PanicParts[part] = makePanicPart(part)
end
local function onRemoved(part)
if PanicParts[part] then
undoPanicPart(PanicParts[part])
PanicParts[part] = nil
end
end
for _,part in pairs(CollectionService:GetTagged("PanicPart")) do
onAdded(part)
end
addedSignal:Connect(onAdded)
removedSignal:Connect(onRemoved)
'''