You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want the script to run for every single part, no matter if for example, player is at stage 2, then goes to stage 5, it doesn’t work. The script seems to only work in a chronological order -
What is the issue? Include screenshots / videos if possible!
The issue is that whenever a player goes from stage 2 to 6, or 7, basically not chronologically, the script just doesnt run -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried tables, didn’t work
Also, if you could, I am a new developer, I’ve messaged the entire script, but what are some coding practices you would recommend to do?
-- Get the StageSpawns folder from the workspace
local spawns = workspace:WaitForChild("StageSpawns")
-- Table to keep track of players who have touched the parts
local touchedPlayers = {}
-- Function to handle the touch event on the part
local function onPartTouch(part, touchedPart)
-- Check if the touched part's parent has a Humanoid (to verify it's a character)
local humanoid = touchedPart.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
-- Get the StageHolder from the part
local stageHolder = part:FindFirstChild("StageHolder")
if not stageHolder then
warn("StageHolder not found")
return
end
-- Get the Confetti particle effect and sound
local particle = script:WaitForChild("Confetti")
local sound = workspace.SoundGame.ObbyWin
local soundClone = sound:Clone()
local particleClone = particle:Clone()
-- Change the BrickColor of the StageHolder and the part
stageHolder.BrickColor = BrickColor.new("Lime green")
part.BrickColor = BrickColor.new("Dark green")
-- Create an invisible part above the stageHolder to hold the particle effect
local invisiblePart = Instance.new("Part")
invisiblePart.Size = Vector3.new(13.095, 2, 7.076)
invisiblePart.Anchored = true
invisiblePart.CanCollide = false
invisiblePart.Position = stageHolder.Position + Vector3.new(0, 7, 0)
invisiblePart.Transparency = 1
invisiblePart.Parent = stageHolder
-- Enable the particle effect on the invisible part
particleClone.Parent = invisiblePart
particleClone.Enabled = true
-- Get the Head part of the character who touched the part
local head = touchedPart.Parent:FindFirstChild("Head")
if not head then
warn("Head not found")
return
end
-- Play the sound on the character's head
soundClone.Parent = head
soundClone:Play()
soundClone.Ended:Wait()
soundClone:Destroy()
-- Wait for 1.5 seconds before destroying the invisible part and disabling the particle effect
task.wait(1.5)
invisiblePart:Destroy()
particleClone.Enabled = false
end
-- Connect the touch event for each part in the StageSpawns folder
for _, part in pairs(spawns:GetChildren()) do
if part:IsA("BasePart") then
part.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not touchedPlayers[part] then
-- Mark the part as touched and prevent multiple triggers
touchedPlayers[part] = true
part:SetAttribute("Touched", true)
task.delay(1, function()
part:SetAttribute("Touched", false)
touchedPlayers[part] = nil
end)
onPartTouch(part, hit)
end
end
end)
end
end
Also, this is a local script in the starterpack, the confetti is parented to it