Showing GUI when touching a part affects all players

Hello! I made a script that, when you touch a part, shows you an image

The thing is, my script just affects everyone, like for ex.: If one touches the part, all players will see the GUI too, including the player that touched it.

Can someone help me so that it only affects one person?

Script (It’s in a LocalScript)

local ts = game:GetService("TweenService")
local inf = TweenInfo.new(0.5,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

local function Tween(Object,Value)
	local OutTween = ts:Create(Object,inf,{ImageTransparency = Value})
	OutTween:Play()
end

local part = game.Workspace.improvedtext
local canhit = true

local chapter = script.Parent.chapter
local info = script.Parent.info

chapter.AnchorPoint = Vector2.new(0, 0)
chapter.Position = UDim2.new(0.277, 0, 0.236, 0)

info.AnchorPoint = Vector2.new(0, 0)
info.Position = UDim2.new(0.356, 0, 0.362, 00)

part.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		if canhit == true then
			canhit = false
			Tween(script.Parent.chapter,0)
			chapter:TweenPosition(UDim2.new(0.277, 0,0.166, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine)
			wait(0.25)
			Tween(script.Parent.info,0)
			info:TweenPosition(UDim2.new(0.356, 0,0.296, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine)
			wait(4)
			Tween(script.Parent.chapter,1)
			chapter:TweenPosition(UDim2.new(0.277, 0, 0.236, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine)
			Tween(script.Parent.info,1)
			info:TweenPosition(UDim2.new(0.356, 0, 0.362, 00), Enum.EasingDirection.Out, Enum.EasingStyle.Sine)
		canhit = true
		end
	end
end)

Make sure your GUI is client sided instead of serversided

Here the thread that could explains your problem

You aren’t checking if the humanoid which touched the part is the player who is running the script, you’re just checking if it’s a humanoid at all. Assuming this is a LocalScript your if statement should be:

local player = game.Players.LocalPlayer 
part.Touched:Connect(function(hit)
	local playerWhoTouched = game.Players:GetPlayerFromCharacter(hit.Parent)
	if playerWhoTouched == player and canhit then
		canhit = false 
		--code
	end
end)

if you are using a server script you can always use a remote event to fire a client-side function that would make the GUI only visible to the player who touched it.

Thank you so much, I was having a headache with this. Thanks to the others as well for helping me as well :slight_smile:

1 Like