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

Audio Script? Anything wrong with it? [Unsolved]

This isn't working. Is there anything I am doing wrong? This script is suppose to find the player who touches the brick (the brick is a doorway and is invisible), and if the audio is playing, it would pause the audio, but if the player's audio is paused, it would play the audio rather.

function onTouch(hit)
	local a = game.Players.LocalPlayer.PlayerGui.Sound.IsPlayed
	local b = game.Players.LocalPlayer.PlayerGui.Sound.IsPaused			
		if a ~= nil then 
		game.Players.LocalPlayer.PlayerGui.Sound:Pause()
		
		else
			
		if b ~= nil then 
		game.Players.LocalPlayer.PlayerGui.Sound:Play()
	
	
	end
	end
	end
script.Parent.Touched:connect(onTouch)

3 answers

✓ Accepted answer
function onTouch(hit)
    local a = game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound.IsPlayed
    local b = game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound.IsPaused
    if a then
        game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound:Pause()
    elseif b then
        game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound:Play()
    end
end
script.Parent.Touched:connect(onTouch)

Would this work?

function onTouch(hit)
    local a = game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound.IsPlayed
    local b = game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound.IsPaused
    if a then
        game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound:Pause()
    elseif b then
        game.Players:getPlayerFromCharacter(hit.Parent).PlayerGui.Sound:Play()
    end
end
script.Parent.Touched:connect(onTouch)

Those properties would always be true or false, never nil. So your condition will always be true. Try this:

function onTouch(hit)
	local a = game.Players.LocalPlayer.PlayerGui.Sound.IsPlayed
	local b = game.Players.LocalPlayer.PlayerGui.Sound.IsPaused
	if a then
		game.Players.LocalPlayer.PlayerGui.Sound:Pause()
	elseif b then
		game.Players.LocalPlayer.PlayerGui.Sound:Play()
	end
end
script.Parent.Touched:connect(onTouch)

6 comments

0

Still didn't work :p

0

You are using localplayer, you have to get the player using GetPlayerFromCharacter method.

0

Check my answer.

0

Yeah but you could be more efficient just by calling the GetPlayerFromCharacter function once instead of 4 times.

0

Thats not a function.. Thats a method

0

Methods are functions. So it is a function since all methods are functions.

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