Help with Checkpoint Sounds!

I’m back on here again in hopes of getting a solution to the problem I have. Currently I have my Checkpoints scripted out with an effect added onto it but still am unsure how to add sound to the checkpoints. My last posts, there were people suggesting that I use remote events but I am still relatively new to scripting so I did a lot of research on the dev forum and roblox resources. I am aware that the devforum has multiple posts that are similar to mine but all of them don’t have solutions and seem to leave me even more confused that I started out with. Here is my Checkpoint Script that I have in ServerScriptService:

local Players = game:GetService("Players");
local RunService = game:GetService("RunService");
local TweenService = game:GetService("TweenService");
local TweenTime = TweenInfo.new(1);
local Checkpoints = workspace:WaitForChild("Checkpoints"):GetChildren();
local sound = game.Workspace.CheckpointNoise


local function FindCheckpoint(character, stage)
	for i, checkpoint in pairs(Checkpoints) do
		local checkpoint_number = tonumber(checkpoint.Name);
		if stage.value == checkpoint_number then
			return checkpoint;
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats";
	leaderstats.Parent = player;
	
	local stage = Instance.new("IntValue");
	stage.Name = "Stage";
	stage.Value = 0;
	stage.Parent = leaderstats;
	
	player.CharacterAdded:Connect(function(character)
		local root_part = character:WaitForChild("HumanoidRootPart");
		local checkpoint = FindCheckpoint(character, stage);
		
		RunService.Heartbeat:Wait();
		root_part.CFrame = checkpoint.CFrame * CFrame.new(0,1,0);
		
		
	end)
end)

for i, checkpoint in pairs(Checkpoints) do
	checkpoint.Touched:Connect(function(touch)
		local humanoid = touch.Parent:FindFirstChild("Humanoid");
		if humanoid and humanoid.Health > 0 then
			local player = Players:GetPlayerFromCharacter(touch.Parent);
			if player then
				local leaderstats = player:WaitForChild("leaderstats")
				local stage = leaderstats.Stage;
				
				local checkpoint_number = tonumber(checkpoint.Name);
				if checkpoint_number - stage.Value == 1 then
					stage.Value = checkpoint_number;
					
					
					local goal = {
						Size = checkpoint.Size + Vector3.new(2, 0.5, 2);
						Transparency = 1;
					};
					
					local checkpoint = checkpoint:Clone()
					checkpoint.Parent = workspace
					
					local tween = TweenService:Create(checkpoint, TweenTime, goal);
					tween:Play();
					
					tween.Completed:Connect(function()
						checkpoint:Destroy();
					end)
				end
			end
		end
	end)
end


I have my checkpoints in a folder called Checkpoints seen here:
Capture
I created a remote event here:
Capture2
And I have a Checkpoint sound that is called CheckpointNoise. From my knowledge that I have learned, I know you’re supposed to create a local script in StarterPlayerScripts that calls to the event. And to make another script that I assume I put in each checkpoint part? The only thing I am unsure of is how to set everything up with the sound besides the:

game.ReplicatedStorage.PlayCheckpointSound.OnClientEvent:Connect(function(plr)

Any direction for helping is greatly appreciated because I really am lost when it comes to this. Thank you to anyone that helps!

4 Likes

To add sound to the checkpoints, you can use the Sound object in Roblox. You can start by creating a Sound object in the workspace or any other parent object, and then setting its SoundId property to the audio file that you want to play when a player touches a checkpoint.

Here’s an updated version of your script that adds a sound effect to each checkpoint:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local TweenTime = TweenInfo.new(1)
local Checkpoints = workspace:WaitForChild("Checkpoints"):GetChildren()

-- Create a sound object for the checkpoint sound effect
local SoundTemplate = Instance.new("Sound")
SoundTemplate.SoundId = "rbxassetid://sound_id_here"
SoundTemplate.Volume = 1
SoundTemplate.Pitch = 1

local function FindCheckpoint(character, stage)
    for i, checkpoint in pairs(Checkpoints) do
        local checkpoint_number = tonumber(checkpoint.Name)
        if stage.value == checkpoint_number then
            return checkpoint
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Value = 0
    stage.Parent = leaderstats
    
    player.CharacterAdded:Connect(function(character)
        local root_part = character:WaitForChild("HumanoidRootPart")
        local checkpoint = FindCheckpoint(character, stage)
        
        RunService.Heartbeat:Wait()
        root_part.CFrame = checkpoint.CFrame * CFrame.new(0,1,0)
    end)
end)

for i, checkpoint in pairs(Checkpoints) do
    local sound = SoundTemplate:Clone() -- Clone a sound object for each checkpoint
    sound.Parent = checkpoint -- Attach the sound to the checkpoint
    
    checkpoint.Touched:Connect(function(touch)
        local humanoid = touch.Parent:FindFirstChild("Humanoid")
        if humanoid and humanoid.Health > 0 then
            local player = Players:GetPlayerFromCharacter(touch.Parent)
            if player then
                local leaderstats = player:WaitForChild("leaderstats")
                local stage = leaderstats.Stage
                
                local checkpoint_number = tonumber(checkpoint.Name)
                if checkpoint_number - stage.Value == 1 then
                    stage.Value = checkpoint_number
                    
                    -- Play the sound effect when the checkpoint is touched
                    sound:Play()
                    
                    local goal = {
                        Size = checkpoint.Size + Vector3.new(2, 0.5, 2),
                        Transparency = 1
                    }
                    
                    local checkpoint_clone = checkpoint:Clone()
                    checkpoint_clone.Parent = workspace
                    
                    local tween = TweenService:Create(checkpoint_clone, TweenTime, goal)
                    tween:Play()
                    
                    tween.Completed:Connect(function()
                        checkpoint_clone:Destroy()
                    end)
                end
            end
        end
    end)
end

In this updated script, a Sound object is cloned for each checkpoint, and attached to the checkpoint using sound.Parent = checkpoint . When a player touches a checkpoint, the sound effect is played using sound:Play() . You can replace the sound_id_here placeholder in the script with the ID of the audio file that you want to use.

4 Likes

Well if you’re trying to play a sound, I would recommend using SoundService.
It runs based on a local script, so do some research in to SoundService and if you get stuck then just ask.

3 Likes

Thank you so much! I had no idea you could do that without remote events :smile: Is there a way to edit the script so that the sound will only play for the person going over the checkpoint and not for the entire server? Or is that something that only remote events can accomplish?

Oh, thank you! I’ll check that out as well!

Take this for example:

Touching a part to stop all other sounds in a sound group then playing a sound that you’re wanting to play…

Code:

-- services
local soundService = game:GetService("SoundService")
local players = game:GetService("Players")

-- variables
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

game.Workspace.Part.Touched:Connect(function(other)
	if player and character == other.Parent then 
		for _, sound in pairs(soundService.SoundGroup:GetChildren()) do
			sound:Stop()
		end
		soundService.SoundGroup.Sound:Play()
	end
end)

To make the sound play only for the person going over the checkpoint, you can modify the Touched event of the checkpoint. Here’s an updated version of the Touched event code:

checkpoint.Touched:Connect(function(touch)
    local humanoid = touch.Parent:FindFirstChild("Humanoid")
    if humanoid and humanoid.Health > 0 then
        local player = Players:GetPlayerFromCharacter(touch.Parent)
        if player then
            local leaderstats = player:WaitForChild("leaderstats")
            local stage = leaderstats.Stage
            
            local checkpoint_number = tonumber(checkpoint.Name)
            if checkpoint_number - stage.Value == 1 then
                stage.Value = checkpoint_number
                
                local goal = {
                    Size = checkpoint.Size + Vector3.new(2, 0.5, 2),
                    Transparency = 1,
                }
                
                local checkpointClone = checkpoint:Clone()
                checkpointClone.Parent = workspace
                
                local tween = TweenService:Create(checkpointClone, TweenTime, goal)
                tween:Play()
                
                tween.Completed:Connect(function()
                    checkpointClone:Destroy()
                end)
                
                local soundClone = sound:Clone()
                soundClone.Parent = checkpointClone
                
                local soundConnection = soundClone.Playing:Connect(function()
                    if soundClone.Playing then
                        soundClone:Stop()
                        soundConnection:Disconnect()
                    end
                end)
                
                soundClone:Play()
            end
        end
    end
end)

In this updated code, a new soundClone instance is created and parented to the checkpointClone instance that was created earlier. A connection to the Playing event of soundClone is also established, which checks if the sound is still playing and disconnects the connection if it is.

This ensures that the sound only plays for the player who triggers the checkpoint, and not for the entire server.

1 Like

For some reason this section says the “soundConnection” is an unknown global.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.