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?
The Problems (within the Code)
Judging upon the information you gave, there are a lot of problems with the codes;
SoundId
s 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
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).
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!
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)