How can you play a sound only once on a touched event?

Here is your server script

local dataStoreService = game:GetService("DataStoreService")
local PlayerService = game:GetService("Players")

local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")
local Checkpoints = workspace:WaitForChild("Checkpoints", 30)

function plrToStage(plr)
	repeat wait() until plr.Character.HumanoidRootPart

	local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))

	plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
end

game.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.Parent = leaderstats

	local success, errorMsg = pcall(function()
		stage.Value = stageStore:GetAsync(player.UserId)
	end)

	if success then
		print("Successfully got "..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end
	if player.Character then
		plrToStage(player)
	end
	player.CharacterAdded:Connect(function()
		plrToStage(player)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
	end)

	if success then
		print("Successfully saved"..player.Name.."'s stage data.")
	else
		warn(errorMsg)
	end
end)

for i, Child in pairs(Checkpoints:GetChildren()) do
	Child.Touched:Connect(function(Hit)
		local Player = PlayerService:GetPlayerFromCharacter(Hit.Parent)
		local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

		if Player and Humanoid then
			local leaderstats = Player and Player:FindFirstChild("leaderstats")
			local Stage = leaderstats and leaderstats:FindFirstChild("Stage")			
			local Number = tonumber(Child.Name)

			if Stage and Stage.Value < Number then
				Stage.Value = Number
			end
		end
	end)
end
1 Like

Thank you so much! I appreciate it! Your awesome I would’ve never done it without you!

1 Like

Your welcome ! :grin:
Make sure to click “Solution” to the related awnser that solved your problem, so other people know it’s solved ^^

Just did it! checked solution where its not supposed to lol

1 Like

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