Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

script detect if a player walks through a part and plays music?

Asked by 3 years ago

So I made a script and it works to a certain level basically meaning it just plays the music but i want it so that when the door part (basically a part) is touched the first time it will play the sound for just that player and when the go through the door again it stops playing the music.

Below is the script

local sound = Instance.new("Sound", workspace)
sound.Pitch = 1 -- Speed of the song (Preffer not to change it)
sound.SoundId = "rbxassetid://4772956740" -- Put the last numerbs of the link of the song(ROBLOX) that you want
sound.Volume = .5 -- Volume(How much power) of the song
sound.Looped = true -- If you want the song to repeat after the song is done
local part = script.Parent
local number = 0

local function onPartTouched(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid and number == 0 then
        sound:Play()
        number = number + 1
    end
end
if number == 2 then
    sound:stop()
    number = 0
end

part.Touched:Connect(onPartTouched)
0
also this has to be attached to the part MEZYPLAYS 0 — 3y

1 answer

Log in to vote
0
Answered by
megukoo 877 Moderation Voter
3 years ago

Your if statement checking if the number is 2 is outside of the .Touched statement, so it will not check if it is 2 on .Touched. To fix this, put it inside of the .Touched event.

local function onPartTouched(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid and number == 0 then
        sound:Play()
        number = number + 1
    end
    if number == 2 then
        sound:stop()
        number = 0
    end
end

You may also want to implement a debounce so the sound does not play a ton of times. Look on the roblox wiki for that.

0
i just did all of it and it still doesn't work like i walk through the door and the sound plays then i walk out and its still playing MEZYPLAYS 0 — 3y
Ad

Answer this question