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

How to make music stop when the player has stepped off the brick?

I've done this and it's given me no errors, but it doesn't stop it either.

local part = script.Parent
local player = game.Players.LocalPlayer
local ws = game.Workspace
local sound = ws.Sound

part.Touched:connect(function(Part)
	sound:Play()
	if part.Touched == false then
		sound:Destroy()
	end
end)

1 comment

0

Touched is an event. Events don't work like that; you can't just use them in if statements. Look into the TouchEnded event, that might work for you.

1 answer

✓ Accepted answer

There are few ways to do that, one way is what Percil said, another simply create a BoolValue Into the part and call it 'Touching' or you can just create a Touching Variable which I do recommend because it's easier. Then in your script add a few lines;

local P = script.Parent -- The part
local Touching = false -- The player is not touching this
local ws = game.Workspace
local sound = ws:WaitForChild('Sound')
local B = ws:WaitForChild('BasePlate') --Change this to the land that the player stands on

P.Touched:Connect(function(H) --When part is touched
	if Touching == true then return end --If touching is already true then end the script
else -- meaning if Touching = false
	sound:Play()
	Touching = true -- sets touching to tue
end)

B.Touched:Connect(function()
	Touching = false
end)

The last function above;

B.Touched:Connect(function()
	Touching = false
end)

Will change Touching back to false if the player steps off the part so if the player steps on a baseplate then touching becomes false and the player can go back on again.

If this helped then please accept answer ;)

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