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

How to play a sound of out PlayerGui?

I am currently trying to expand upon one of my test powerups by making a theme play when it is touched.

--Declarations-----
local speedBoost = script.Parent
local boostSpeed = 48
local cooldown = 10
local speedDuration = 5
local canRun = true
-------------------------------

---------Part controlling the powerup effect------------
local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid and canRun then
		canRun = false
		humanoid.WalkSpeed = boostSpeed
		speedBoost.Transparency = .5
		wait(speedDuration)
		humanoid.WalkSpeed = 16
		wait(cooldown)
		speedBoost.Transparency = 0
		canRun = true
	end
end
speedBoost.Touched:connect(handleTouch)
-----------------------------------------------------

--------------Part attempting to control music-----------
speedBoost.Touched:connect(function(hit)
	
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local plrgui = game:GetService('Players').LocalPlayer.PlayerGui
		print(player)
		local starMan = plrgui.starMan
		starMan:Play()
	end
end)
-------------------------------------------------------------------------

This works only while in Studio Play mode for the obvious reason of LocalPlayer being used. When I try to link it to the player's GUI by replacing LocalPlayer with the variable player I get an error stating that player is not a valid member of Players, despite it clearly printing out the name of the character (In Studio's case, Player1) as such

--------------Part attempting to control music-----------
speedBoost.Touched:connect(function(hit)
	
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local plrgui = game:GetService('Players').player.PlayerGui --Breaks
		print(player) --Prints Player1
		local starMan = plrgui.starMan
		starMan:Play()
	end
end)

Having the server call local sounds from a player GUI works perfectly, I've already done the testing in the Server mode of Studio where these put into the command bar game.Players.Player1.PlayerGui.starMan:Play() plays the theme on my computer and game.Players.Player.PlayerGui.starMan:Play()Even played it on my IPad through the Roblox Developer app.

At this point I'm stuck with what to do, I can't progress with my future plans for the powerup without the music being able to play in its own right.

Any help is appreciated! Thanks

~MBacon15

1 comment

0

soundposition?

1 answer

✓ Accepted answer

The variable player goes directly to the player object. So, you're essentially looking for game.Players.game.Players.Player.PlayerGui. If you replace that with player.PlayerGui it should work.

--------------Part attempting to control music-----------
speedBoost.Touched:connect(function(hit)

    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local plrgui = player.PlayerGui
        print(player) 
        local starMan = plrgui.starMan
        starMan:Play()
    end
end)

Hope this helped.

2 comments

0

Thanks!

0

No problem.

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