Recently I tried to make a script like this:
If the player touches the part:
-Play Sound
-Change all blocks material to smooth plastic
Unfortunately, when I stand on the block, the script repeats itself over and over again (the sound is constantly playing).
Here's the script:
script.Parent.Touched:Connect(function(Hit)
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
game.Workspace.Sound:Play()
game.Workspace.KillPart1.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart2.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart3.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart4.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart5.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart6.Material = Enum.Material.SmoothPlastic
wait(3)
Hit.Position = game.Workspace.Spawn1.Position
end
end)
I tried to do this with RemoteEvent, but then found it wouldn’t change anything anyway.
The script is a normal script (not a local script).
Does anyone know how to check if a player touches this block one time?
I just ask you not to complicate the script as I’m not very advanced.
local debounce=false
script.Parent.Touched:Connect(function(Hit)
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
if not debounce then
debounce=true
game.Workspace.Sound:Play()
game.Workspace.KillPart1.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart2.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart3.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart4.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart5.Material = Enum.Material.SmoothPlastic
game.Workspace.KillPart6.Material = Enum.Material.SmoothPlastic
game.Workspace.Sound.Ended:Wait()
debounce=false
Hit.Position = game.Workspace.Spawn1.Position
end
end
end)
-- create a variable called debounce that is set to nil
local debounce = nil
-- when something touches the script parent
script.Parent.Touched:Connect(function(touchedPart)
-- if the debounce is = to true then we exit the function early and ignore the touch
if debounce == true then return end
-- we assume the parent is a character but we are not sure yet
local character = touchedPart.Parent
-- we try to find the player using the character
local player = game.Players:GetPlayerFromCharacter(character)
-- if the player is nil this means it was not a players character that touched the part so we exit the function
if player == nil then return end
-- set debounce to true to block this function if it gets called again
debounce = true
-- Play the sound
game.Workspace.Sound:Play()
-- loop every child inside the KillParts folder and set there material to SmoothPlastic
for i, child in ipairs(game.Workspace.KillParts:GetChildren()) do
child.Material = Enum.Material.SmoothPlastic
end
-- wait 3 seconds
task.wait(3)
-- move the character to Spawn1
character:PivotTo(game.Workspace.Spawn1.CFrame)
-- set debounce back to nil so this function will no longer get blocked and can be called again
debounce = nil
end)