How to get player from Touched (Server Script)

Hi guys,

I have parts through out my game that I use to control other elements of the map. I want to access the PlayerGUI of the Player that touched one of these parts, however, I cannot seem to figure it out, and honestly I am not sure if it is possible to begin with.

This is the script that I am currently testing with:

Summary
local part = script.Parent

function onTouched(h)
	local h = h.Parent:FindFirstChild("Humanoid")
	local character = h.Parent.Name
	print(character)
	local player = game.Players.character
	local dialogueUI = player.PlayerGUI.DialogueUI
	if h ~= nil then
		workspace.Ignored.Barriers.Barrier1:Destroy()
		part:Destroy()
	end
end

script.Parent.Touched:Connect(onTouched)

When using this script, I get this error:
image

If anyone can get me moving in the right direction, that’ll be great!

script.Parent.Touched:Connect(function(player) 
	local h = player.Parent:FindFirstChild("Humanoid")
	local character = h.Parent
	local dialogueUI = player.PlayerGui.DialogueUI
	if h ~= nil then
		workspace.Ignored.Barriers.Barrier1:Destroy()
		part:Destroy()
	end
end)
2 Likes

image

Using your code

I was able to find the Player using this:

local player = game.Players:GetPlayerFromCharacter(character)

PlayerGUI doesn’t exist, it’s PlayerGui