Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 ;)

1 comment

0

Edited.

1 answer

✓ Accepted answer

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)

3 comments

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.

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.