The Problems (within the Code)
Judging upon the information you gave, there are a lot of problems with the codes;
- You can not call 'LocalPlayer' from a Server-Sided script
- 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
- 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
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)
if Player and Player:IsA("Player") then
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")
if ThePlayersPlayerGui then
local ThePlayersSound = ThePlayersPlayerGui:FindFirstChild("Sound")
if ThePlayersSound then
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()
ThePlayersSound.SoundId = "rbxassetid://142626808"
wait(.1)
ThePlayersSound:Play()
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!
2 comments
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!
No problem, glad to help. :)