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

[RESOLVED]Brick not changing sound volume on touch, help?

Asked by 9 years ago

The sound is located in the PlayerGui and is named "OutdoorAmbiance," however after hours of frustrating work, I just can't get it to work, even after using the information I got from the roblox forums.

local parent = script.Parent

parent.Touched:connect(function(hit)
    if hit and hit:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit) then
        local p = game.Players:GetPlayerFromCharacter(hit)
        local c = hit.Parent
        local sound = p.PlayerGui:findFirstChild("OutdoorAmbiance")
        sound.Volume = 0.3
    end
end)

Please at least try and explain what you fix please ;)

0
Edited. Redbullusa 1580 — 9y

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

The problem with your script is that your "if" statement couldn't find "Humanoid" in the object that it hits. Normally, the Humanoid can be found in the character, and that's what you're (more than likely) trying to aim for.

But instead, the script is trying to find "Humanoid" in the object it hits.

For example, if "Left Leg" hits this brick, "Left Leg" will become the argument hit. Then the script will try and find a "Humanoid" in "Left Leg". If it's nonexistent, the script will skip the "if" statement, because the condition is not satisfied.

Therefore, the audio's volume doesn't change.

local parent = script.Parent

parent.Touched:connect(function(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
        local p = game.Players:GetPlayerFromCharacter(hit.Parent)
        -- local variable "c" is never used in this scope; refrain from making dead variables.
        local sound = p.PlayerGui:FindFirstChild("OutdoorAmbiance")
        sound.Volume = 0.3
    end
end)
0
The sound remains unchanged. I see what you're trying to tell me however when I put the script in my brick, it still doesn't alter the volume. XxDarkMiragexX 45 — 9y
1
Be sure to double check on my work, and see if it makes sense. The mistake on my part is on line 5: local variable "p" should be "game.Players:GetPlayerFromCharacter(hit.Parent)". It's valid and consistent with the condition. Redbullusa 1580 — 9y
0
Thank you! I'm trying to get around and learn scripting, so spotting errors in these scripts is quite difficult for me at the moment. XxDarkMiragexX 45 — 9y
Ad

Answer this question