Reffering to plr.Name not working (Sometimes)

I’m having troubles with some of my scripts not finding the name of the player touching. It’s a local script inside startergui. What’s weird is that it sometimes work and sometimes it doesn’t.

ArtMuseuma.Touched:Connect(function(hit)
	
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr.Name ==
		player.Name then
	
	if MyTouch == false then
		MyTouch = true
		local NameOfTeleport = "ArtMuseumTeleport"


		game.ReplicatedStorage.Teleport.NoCheckTeleport:FireServer(NameOfTeleport)
		CircularHole:TweenSize(UDim2.new(0.004,0,0.01,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Right:TweenPosition(UDim2.new(0.756,0,.5,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Left:TweenPosition(UDim2.new(0.249,0,.5,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Top:TweenPosition(UDim2.new(.5,0,.245,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Bottom:TweenPosition(UDim2.new(.5,0, .754 ,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)


		wait(1)

		Middle.Visible = true

		wait(.2)
		Middle.Visible = false
		CircularHole:TweenSize(UDim2.new(1.5,0,2.5,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Right:TweenPosition(UDim2.new(1.5,0,.5,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Left:TweenPosition(UDim2.new(-0.49,0,.5,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Top:TweenPosition(UDim2.new(.5,0,-1,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		Bottom:TweenPosition(UDim2.new(.5,0, 2 ,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 1)
		wait(1)
		MyTouch = false
	end

end

First of all, please fix your indentation, your if statement should not look like that. Second of all, you need to also check if plr exists with something like if plr and plr.Name == player.Name then. Lastly, I suggest you utilize the player’s UserId instead of their name for better accuracy.

Yea sorry the if statements isn’t supposed to look like that I just tried to figure our which part of the script that was the problem. And I will try your suggestion.

What is player by the way? The value player that you’re cross checking with the player that touched.

local ArtMuseuma = game.Workspace.TouchArtMuseum

local ArtMuseumBackToCity = game.Workspace.ArtMuseumBackToCity

local CircularHole = script.Parent.LoadingTeleports.ImageLabel

local Right = script.Parent.LoadingTeleports.Right

local Left = script.Parent.LoadingTeleports.Left

local Top = script.Parent.LoadingTeleports.Top

local Bottom = script.Parent.LoadingTeleports.Bottom

local Middle = script.Parent.LoadingTeleports.Middle

local player = game.Players.LocalPlayer

local MyTouch = false

Ohh, okay, you can just do if plr == player then.

1 Like