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

What is wrong with my onTouched Gui button?

Asked by 9 years ago

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

Log in to vote
2
Answered by
TopDev 0
9 years ago

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)



0
Awesome, thanks! Sadly, someone rated you down, but I rated you up so you stay even. SlickPwner 534 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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.

Answer this question