Hey developers, once again I’m hitting the scripting support category because I couldn’t get an answer.
So, here’s the question:
How do you actually make a sound when a player hits a checkpoint, and for the sound to play just for them?
I’ve had mentions about RemoteEvents, however I haven’t seen anybody make an in-depth explanation about it, so it’s hard to understand what people are trying to say.
In addition, I want to know how to play that sound once (so when you touch the checkpoint for the first time, it will play the sound, however if you touch the checkpoint after you’ve already hit it then it won’t actually play the sound)
Here is my current script:
local thing = script.Parent
local function confetti()
for i, v in pairs(thing:GetDescendants()) do
if v:IsA("ParticleEmitter") then
v.Enabled = true
end
end
wait(0.3)
for i, v in pairs(thing:GetDescendants()) do
if v:IsA("ParticleEmitter") then
v.Enabled = false
end
end
end
thing.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if workspace.sounds.LevelComplete.IsPlaying == false then
workspace.sounds.LevelComplete:Play()
end
confetti()
end
end)
Everything works but the sound playing locally, and I’d like to figure out the addition towards the sound playing only once.
The sound in question is ‘LevelComplete’.
Thanks, and I hope to finally get an answer!
Maybe make a Touched event that plays the sound and then makes itself disabled, or add a debounce so it doesn’t play again.(In a separate server script)
Here is a simple solution:
Create an event in ReplicatedStorage called “PlayLevelComplete” (I have it in a folder in the ReplicatedStorage called “Events” but you can change that if you want).
Then, insert the following code into the function: thing.Touched:Connect(function(hit) after the if workspace.sounds.LevelComplete.IsPlaying == false then part:
local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local Events = game.ReplicatedStorage:WaitForChild("Events")
local PlayLevelComplete = Events:WaitForChild("PlayLevelComplete")
PlayLevelComplete:FireClient(player)
Now, create a local script in StarterPlayer and also have the “LevelComplete” audio in there (In this example I also have it in a folder called “Audio”).
Last, Have the local script with the following code:
local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local Events = game.ReplicatedStorage:WaitForChild("Events")
local PlayLevelComplete = Events:WaitForChild("PlayLevelComplete")
local Audio = script.Parent.Audio
PlayLevelComplete.OnClientEvent:connect(function()
Audio.LevelComplete:Play()
end)
If you want this to occur once, just make the script in your question delete itself after it’s done. If you want to keep the script, you can also make it disable itself. One other thing you could do is have a Boolean value as true at first and false once it’s finished, and then make the script check if it’s true or false. Also, don’t forget to have looped = false in the audio.
2 Likes
Thanks for attempting to help, but it says this even though I put the folder as well as the audio in:
I have it working in the place I used to test it and it works. With the code I shared with you, the folder audio and local script are in “starterplayerscripts” within “playerscripts” And they are separate from each other.
Here’s what it looks like in my place.