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

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

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

✓ Accepted answer

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

2 comments

0

It only works in studio! why wont it work in the roblox player?

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

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