Using a touched event to trigger UI

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	if character then
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			local playerGui = player:FindFirstChild('PlayerGui')
			if playerGui then
				local hud = playerGui:WaitForChild('HUD')
				if hud then
					print(4)
					local shop = hud:FindFirstChild('ShopXY')
					if shop then
						print(5)
						if not debounce then
							debounce = true
							shop.Visible = true
							shop:TweenPosition(UDim2.new(0, 0, 0.5, 0), 'Out', 'Back', 1, true)
							local humanoid = character:FindFirstChild('Humanoid')
							if humanoid then
								humanoid.WalkSpeed = 0
								humanoid.JumpPower = 0
							end
							wait(1)
							debounce = false
						end
					end
				end
			end
		end
	end
end)

Currently, I am trying to create a part that when touched opens up a shop UI. It stops at the local hud variable. Originally I had it as FindFirstChild(), but since that wasn’t working I changed it to WaitForChild(). Still not working. There is a ScreenGui called HUD (spelt exactly how it is written in the code) and it is inside the PlayerGui, so I don’t know why it ain’t picking it up.

I would think that if it can pick up the PlayerGui, that it can then see what’s inside it as well (seen as it can see what’s inside the backpack, etc.)

Is this in a LocalScript or regular Script?

Regular

PlayerGui content does not replicate to the server due to FilteringEnabled. PlayerGui contents are only visible to the client. Plus, it’s best practice that User UI should be handled solely by the client. You should put all of this code into a LocalScript, as there doesn’t seem to be any server-required function calls.

Chucking the code in a LocalScript did nothing :confused:

Where is the script located?

This should work. Run this from a Local Script inside of PlayerGui. Don’t use remote events, unnecessary use of server memory.

local player = game.Players.LocalPlayer
local gui = script.Parent.GUI
local partToHit = workspace.Part
local canShow = true

player.CharacterAdded:Connect(function(character)
    repeat wait() until character.PrimaryPart ~= nil
    character.PrimaryPart.Touched:Connect(function(hit)
        if hit == partToHit and canShow then
            canShow = false
            gui.Visible = true
            wait(2)
            canShow= false
        end
    end)
end)
1 Like

There’s no need. This should all be done client side.

2 Likes

EDIT: 4-hour ninja’d by MrAsync.

Oh sweet lord that is some massive, unnecessary checking. Just use a LocalScript, connect up to touched and show the Gui locally. You can’t make such changes on the server because it won’t work a second time around if the client makes a change and it’s generally a bad thing to put any server-side code in a UI (it should be purely client-sided). You don’t need a remote either - that’s a useless layer you can skip.

1 Like

Exactly what I submitted above.

You’re absolutely right, but look what it says here.

@Nightrains Probably because he didn’t write the code properly. It’s completely fine to run this operation in a LocalScript. He just needs to change the code.

@MrAsync Didn’t catch that, posted without reading replies.

1 Like

This should work. Run this from a Local Script inside of PlayerGui. Don’t use remote events, unnecessary use of server memory.

local player = game.Players.LocalPlayer
local gui = script.Parent.GUI
local partToHit = workspace.Part
local canShow = true

player.CharacterAdded:Connect(function(character)
    repeat wait() until character.PrimaryPart ~= nil
    character.PrimaryPart.Touched:Connect(function(hit)
        if hit == partToHit and canShow then
            canShow = false
            gui.Visible = true
            wait(2)
            canShow= false
        end
    end)
end)

This message is repeated.

Colbert said he didn’t see it.

I have a scroll up button on my mouse, you know.

Anyway, this can be continued in DMs. Starting to derail from OP.