How to make this UI function when touching a part?

I have a script that opens a dialogue GUI upon activating a ProximityPrompt.
I tried to modify the script so that it would work when touching a part.
I realized that my problem is that plr is getting it’s properties from the part, and not the LocalPlayer anymore. If there’s a way to make it where touching the part will get the LocalPlayer’s PlayerGUI please let me know.

local say = {"This door is locked."}
local debounce = false

script.Parent.Touched:Connect(function(plr)
	if debounce == false then
				debounce = true
				script.Parent.Interact:play()
				wait(.125)
				plr.PlayerGui.TextUI.Background.Nameplate.Tag.Text = " "
					plr.PlayerGui.TextUI.Enabled = true
					plr.PlayerGui.TextUI.Enabled = true 
					for i, v in pairs(say) do
						for i = 1, string.len(v) do
							wait(0.05)
							script.Parent.HenryVoice:play()
							plr.PlayerGui.TextUI.Background.Dialogue.Text = string.sub(v, 1, i)
						end
						wait(string.len(v) / 25)
					end
					plr.PlayerGui.TextUI.Enabled = false
					debounce = false
				end
end)
1 Like

You can get the player from the character.

local players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local character = humanoid.Parent
		local player = players:GetPlayerFromCharacter(character)
	end
end

Although if it’s a local script, you should use players.LocalPlayer.

1 Like