Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a step on part plays sound once script?

So lets say someone steps on a part. Then they hear a sound, but when they try to step on it again nothing happens. And when the sound played the first time nobody but the player who stepped on it hears it.

2 comments

0

You keep asking the same question. Apply the last question you asked about a brick disappearing with playing a sound.

0

well its very complicated

2 answers

You'll have to play the sound locally on the client of the person who stepped on it.

Simply stated, you should use a touched event, then play the sound on the client using some remote event or something.

Something like this might work:

Be sure to insert the RemoteEvent object into replicated storage

--Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RemoteEvent = ReplicatedStorage:WaitForChild('RemoteEvent")

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		--get the player
		local player = Players:GetPlayerFromCharacter(character)
		--fire the event on the client
		RemoteEvent:FireClient(player)
	end
end)

--local script
--this should be parented to the player 

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild('RemoteEvent")

local Part = workspace:WaitForChild("Part")
local Sound = Part:WaitForChild("Sound")

RemoteEvent.OnClientEvent:Connect(function()
	if Sound then
		Sound:Play()
		--wait for the sound to stop playing
		Sound.Ended:Wait()
		--destroy it so that it cant be used again on this client
		Sound:Destroy()
	end
end)

2 comments

0

Where do I put this script? And is it a local script?

0

Where does the sound or sound ID go?

If you only want the player who stepped on it to hear, I'd suggest using a Local Script, let's get to the code:

local hasStepped = false
part.Touched:Connect(hit)
	if hasStepped == true then
		return
	end
	local humanoid = hit.Parent.FindFirstChild("Humanoid")
	if humanoid then
		sound:Play()
		hasStepped = true
	end
end)

Now let's break the code down:

local hasStepped = false

First of all we declare a variable called hasStepped, to store whether the player has or hasn't stepped on the part yet.

part.Touched:Connect(hit)

In this section, we tell the part to connect to a function when it gets touched using "hit" as an argument. If you wonder what "hit" is, try doing:

part.Touched:Connect(function(hit)
    print(hit)
end)

It will print out a part of the player's character, and every body part is the child of the character itself.

if hasStepped == true then
	return
end

Now we check if the hasStepped variable is true, if it is, it's going to skip the block of code, not playing the audio.

local humanoid = hit.Parent.FindFirstChild("Humanoid")

Declaring a variable for the humanoid so we can check if it's true on the next line of code.

if humanoid then
	sound:Play()
	hasStepped = true
end

Now if it is a humanoid, we play the sound and set hasStepped to true, meaning that the function will never run again.

You can also use:

script:Destroy()

But instead of using a variable, it destroys the script and you're never able to use it again.

1 comment

0

Where do I put the sound or where does the sound ID go?

This discussion is preserved as it was. New answers, comments, and votes are closed.