On touch then open gui

Im trying to make a script so when they touch a part it opens a gui but it opens for every player? can someone help?


script.Parent.Touched:Connect(function()
	game.ReplicatedStorage.Opentp:FireClient()
end)

script.Parent.Touched:Connect(function()
      local Humanoid = script.Parent:FindFirstChild("Humanoid") 
     if Humanoid then 
         local Character = Humanoid.Parent
         local Player = game.Players:GetPlayerFromCharacter(Character)
         if Player ~= nil then
        game.ReplicatedStorage.Opentp:FireClient(Player)
        end
     end
end)

Ignore format, wrote on Forum.

it is saying index nil parent on line

  local Character = Humanoid.Parent

Whoops my mistake,

script.Parent.Touched:Connect(function(Hit)
      local Humanoid = Hit.Parent:FindFirstChild("Humanoid") 
     if Humanoid then 
         local Character = Humanoid.Parent
         local Player = game.Players:GetPlayerFromCharacter(Character)
         if Player ~= nil then
        game.ReplicatedStorage.Opentp:FireClient(Player)
        end
     end
end)
2 Likes

There’s a small problem with your code, being the fact that you’re not actually getting what is touching the part, but rather the parent of the script itself.

I’d also say you don’t need a variable specific for the character, nor do you actually need to check if player is unequal to nil, as simply checking if there is not “player”, then return end, which just makes the code not finish.

script.Parent.Touched:Connect(function(hit)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end

	game.ReplicatedStorage.Opentp:FireClient(player)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.