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

What is wrong with my onTouched Gui button?

I'm trying to make a brick that pops a Gui onto your screen when you touch the brick. It's not doing anything though. I've fixed it to the point that no errors appear in the Output, but it still doesn't do anything.


function onTouched(hit)
	if hit.Parent.Humanoid ~= nil then
		player = hit.Parent.Parent.Name
		for i,v in pairs(game.Players:GetChildren()) do
			if v.Name == player then
				plr = v
				EnchantBG = plr.PlayerGui.ScreenGui.EnchantBG
					if EnchantBG.Visible == false then
					EnchantBG.Visible = true
					EnchantBG.Position = UDim2.new(0,545,0,-550)
					EnchantBG:TweenPosition(UDim2.new(0,545,0,60), "Out", "Bounce", 1)
					else
					EnchantBG.Visible = true
					end
			end
		end
	end
end
script.Parent.Touched:connect(onTouched)



2 answers

✓ Accepted answer

Try this, I haven't tested it yet but it should work, let me know if it dosen't.

function onTouched(hit)
    if hit.Parent.Humanoid ~= nil then
                player = game.Players:GetPlayerFromCharacter(hit.Parent)
                EnchantBG = player.PlayerGui.ScreenGui.EnchantBG
                    if EnchantBG.Visible == false then
                    EnchantBG.Visible = true
                    EnchantBG.Position = UDim2.new(0,545,0,-550)
                    EnchantBG:TweenPosition(UDim2.new(0,545,0,60), "Out", "Bounce", 1)
                    else
                    EnchantBG.Visible = true
                    end
            end
end
script.Parent.Touched:connect(onTouched)



1 comment

0

Awesome, thanks! Sadly, someone rated you down, but I rated you up so you stay even.

Line 02: You're checking if the humanoid ~= nil, but before you do that, you're already assuming it's NOT nil. This may seem hard to wrap your head around, but it's true. Therefore you must do:

hit.Parent:FindFirstChild("Humanoid")

FindFirstChild() is a good way to check if something exists, even if you're not sure it does.

Line 03 If a character touches the brick with his left leg, that means that 'hit' is the character's left leg. Therefore hit.Parent.Parent is game.Workspace.

To get the player from a character, use GetPlayerFromCharacter().

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

What that method does is try to get the player from the argument you put in parenthesis, which should be a character. If there is no player, it returns nil. If you have NPCs in your game, you should use an if statement to make sure it does not return nil. This also makes the for loop on line 04 unnecessary.

Line 08: If EnchantBG.Visible = false, then set it to true. Otherwise, set it to true. The else is unnecessary, unless you meant to set it to false.

Also, you may need a debounce, you may not. It would probably be good for you to look into it though.

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