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

SoundId won't change in playerGui?

Asked by
yoshi8080 445 Moderation Voter
8 years ago

I wanted the sound located in the PlayerGui to change the soundId when touching a part in workspace. When the part is touched, the music stays the same. Script located in StarterGui

local player = game.Players.LocalPlayer
local gui = player.PlayerGui
local music = Instance.new("Sound",gui)
local Id = "rbxassetid://"
wait(1)
music.SoundId = Id.."142626808"
music:Play()

Script located in part

local Id = "rbxassetid://"
local player = game.Players.LocalPlayer
local gui = player.PlayerGui
local sound = gui:FindFirstChild("Sound").SoundId
script.Parent.Touched:connect(function(hit) 
sound = Id.."142626808"
end) 

Both scripts are LocalScripts, please help?

2 answers

Log in to vote
0
Answered by 8 years ago

The Problems (within the Code)

Judging upon the information you gave, there are a lot of problems with the codes;

  1. You can not call 'LocalPlayer' from a Server-Sided script
  2. The Variable 'hit' for the function specifies the Part, in this case the Arm/Leg, that touched the Part it does not return the Player itself
  3. Lines 4 and 6 (in the second code) are sort-of right, but only if your having the Variable saving the SoundIds Value, otherwise, that code is way off :(

The Solution to the problem (to fix the code, in other words)

However, these problems can be fixed, by doing some re-coding. :)

The ways to fix it, is what I have listed before, by fixing those problems. :)

Now, let me explain;

Fixing the problems and the code

Instead of doing what you did for hit, why not check it's Parent (if it's not a Part that disappears automatically)?

script.Parent.Touched:connect(function(hit)
    if not hit.Parent then return end --In-case your wondering about this code, sometimes a Part has a disappear (which destroys or sets the Part's Parent property to 'nil', which could break the code
end)

The next step would to be is to remove lines 2-4 in the second code; Those are not needed yet. After so, let's set up a variable in the Part;

script.Parent.Touched:connect(function(hit)
    if not hit.Parent then return end
    local Player = game.Players:FindFirstChild(hit.Parent.Name) --The 'FindFirstChild' method returns a Boolean if the Child exists within a specific Parent; If exists, returns the child, if not, returns 'nil'
    if Player and Player:IsA("Player") then --Checks to see if the Player exists, and if it is a Player (many may consider this bad code, but I do so just-in case :) )

    end
end)

Now, we have the code set up, to check if a Player is existant, but not the PlayerGui/Sound/ect., this is a simple process, all we have to do is set up Variables to see if they exist, just like with the Player variable.

script.Parent.Touched:connect(function(hit)
    if not hit.Parent then return end
    local Player = game.Players:FindFirstChild(hit.Parent.Name)

    if Player and Player:IsA("Player") then
        local ThePlayersPlayerGui = Player:FindFirstChild("PlayerGui") --Same as the 'Player' Variable; It will return the PlayerGui if it exists within the Parent, in this case 'Player', but if not, returns nil

        if ThePlayersPlayerGui then --This will check to see if the 'PlayerGui' is existant within the 'Player'
            local ThePlayersSound = ThePlayersPlayerGui:FindFirstChild("Sound") --Same here as the 'ThePlayersPlayerGui' and 'Player' Variables

            if ThePlayersSound then --Same as with the 'ThePlayersPlayerGui' variable

            end
        end
    end
end)

Now, since that problem is fixed, now goes onto fixing the Sound; Before, you set it to Automatically change it, but, it does not play the desired ID after that; You did not call anything to play the ID, and you did not set anything up to stop the original ID either; However, this can be fixed by fixing the things I have listed before.

script.Parent.Touched:connect(function(hit)
    if not hit.Parent then return end
    local Player = game.Players:FindFirstChild(hit.Parent.Name)

    if Player and Player:IsA("Player") then
        local ThePlayersPlayerGui = Player:FindFirstChild("PlayerGui")

        if ThePlayersPlayerGui then
            local ThePlayersSound = ThePlayersPlayerGui:FindFirstChild("Sound")

            if ThePlayersSound then
                ThePlayersSound:Stop() --This will stop playing the original ID that was playing before
                ThePlayersSound.SoundId = "rbxassetid://142626808" --This will change the SoundId to the different ID
                wait(.1) --Waits 10th of a second, to give the code a chance to set it
                ThePlayersSound:Play() --Plays the new ID
            end
        end
    end
end)

And, ta-da.. It's fixed.. I think. :P

Information left out

  1. IsA - The IsA function is a method that checks the Childs' (or Instances') ClassName Variable (I'm guessing) to see if it as the Argument given, if so, it will return a Boolean (true = Is as the Argument given, false = Is not as the Argument given).

  2. If Statement - The If statement checks to see if the Condition (or Variable/Value) is equal to the Argument, this checks by Boolean; If the Condition is equal to it's argument, it takes the Variable/Value as true, and runs the specific code, if not, then it takes it as false, and does not run any code (unless your using else or elseif). However, a common problem with this is that users think it checks the Condition (only typed once in the If statement) to multiple arguments, let me show you real quick;

if Variable == "1" or "2" or "3" then
    return print("These all are true no matter what, right?")
end

That is the incorrect Syntax when using the If statement; Use the same condition for each Argument there is;

if Variable == "1" or Variable == "2" or Variable == "3" then
    return print("That seems right than before! What were you thinking, TheAlpha?!")
end

This is what the incorrect Syntax for the If statement would look like.

I wrote this out real quick because some Scriptors misinterpret how the If statement is used, I used this just-in-case to clarify how to properly use it. :)

Honestly, I felt like this was a terrible explanation, and I'm sorry if this didn't solve your problem. :(

Hope this helped!

0
Oh by the time you answered this question, I'd asked a friend to fix it, and he did. This would be great information for me to look back on! yoshi8080 445 — 8y
0
No problem, glad to help. :) TheeDeathCaster 2368 — 8y
Ad
Log in to vote
0
Answered by
Mystdar 352 Moderation Voter
8 years ago

Normal script in the part:

function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") ~= nil
        Player = game.Players[hit.name]
        print (Player.name)
        Sound = Player.PlayerGui:findFirstChild("Sound").SoundId -- Note the "f" in "find" is lowercase
        sound = "42626808"
    end
end
script.Parent.Touched:connect(onTouched)
0
'findFirstChild' is the same as 'FindFirstChild'; The only difference is the capitalization. This is not the problem to his code. :P TheeDeathCaster 2368 — 8y

Answer this question