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

Script that plays music when certain part touches it?[CLOSED]

Asked by 8 years ago

This is my first question on this website so please consider that if i make any mistake. I have a script that when something touches the can collide = false block it plays music but this applies with other objects such as other players. How can i make it so it knows that the local player is touching the area? here is what i got so far

function onTouched()
    game.Workspace.ZONEONEMUSIC:Resume()


end
game.Workspace.ZONEONE.Touched:connect(onTouched)
function endtouch()
    game.Workspace.ZONEONEMUSIC:Pause()

end
game.Workspace.ZONEONE.TouchEnded:connect(endtouch)




-- this part is when you teleport outside of the block
function Tele()
    game.Workspace.ZONEONEMUSIC:Pause()


end



game.Workspace.TeleportMorphZONE.Touched:connect(Tele)


1 answer

Log in to vote
-1
Answered by 8 years ago

This question is easy. Use 'if' statements to check "if" the part that touched it [ is a / or contains a] 'Humanoid'. Using 'if' statements helps the code know what to check before running the code.

Here is what it should look like:

This is for future reference. This is written for you to learn. Please appreciate the time I took to write this for you.

function onTouched(hit) -- the Variable 'hit' Could be anything you want it to be. You can change this anything to your liking.
    if hit and hit.Parent:FindFirstChild("Humanoid") then -- 'hit' is the part that touched it that triggered the event.
        -- In this 'if statement' the code is looking for a 'Humanoid' inside the part's Parent which should be a model, by using the ":FindFirstChild()" method.
        -- If the 'if statment' is true then the code will run.
        game.Workspace.ZONEONEMUSIC:Resume()
    end
end
game.Workspace.ZONEONE.Touched:connect(onTouched)


Now here's the full script:

function onTouched(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") then 
        game.Workspace.ZONEONEMUSIC:Resume()
    end
end
game.Workspace.ZONEONE.Touched:connect(onTouched)

function endtouch(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") then
        game.Workspace.ZONEONEMUSIC:Pause()
    end
end
game.Workspace.ZONEONE.TouchEnded:connect(endtouch)

function Tele(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") then
        game.Workspace.ZONEONEMUSIC:Pause()
    end
end
game.Workspace.TeleportMorphZONE.Touched:connect(Tele)

And for the code so that it knows its a local player then you have to use a Local Script for that.

Here, I have given you an idea how your code should start. Note: This can only be done in local scripts:

local player = game.Players.LocalPlayer
local Character = player.Character

0
It only works in studio! why wont it work in the roblox player? only12hours 5 — 8y
0
Hmm, Since scripts run before the player joins you should add 'wait()' at the very top of the script. This will make the script to wait enough for the player GeezuzFusion 200 — 8y
Ad

Answer this question