Repeating the same action over and over

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.

2 Likes

Of course use a debounce and check if the material is equal to smooth plastic

Or you could also rename the part so you would only check if the name is equal to already used or something like that.

2 Likes
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)
3 Likes

here is a more complex version ʕ⌐■ᴥ■ʔ

-- 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)
2 Likes

An error pops up on the output:

15:38:45.401 Workspace.KillPart1.Script:13: attempt to call a nil value

This error then repeats itself several times.

Zrzut ekranu 2022-05-5 o 15.42.08

2 Likes

put

game:GetService('Players')

in front of it

1 Like

@Qin2007 @5uphi Now an error pops up on output:

Cframe is not a valid member of Part “Workspace.Spawn1”

the c and the f are both capital letters
only CFrame will work

1 Like