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

How to play a sound of out PlayerGui?

Asked by 8 years ago

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

0
soundposition? UniversalDreams 205 — 8y

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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.

0
Thanks! MBacon15 97 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question