Attempted to index nil with _pvp

I want this script to check if you have PVP enabled, if you do, it will give you a message, but it shows the error “Attempted to index nil with _pvp.”

script.Parent.Touched:Connect(function(hit)
	
	
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
	if plr._pvp.Value == false then
		
			for i = 1,20 do
				plr.PlayerGui.Alert.Status.TextTransparency =plr.PlayerGui.Alert.Status.TextTransparency + .1
				
			end			
		end
	end)```

This is unreliable code. The touched function will operate if whatever part touches ANYTHING. Therefore you need to actually ensure what it’s touching is a player first off. If it is touching the floor, stuff in it, or other things of sort, there isn’t a player associated to that so it will of course return nil.

1 Like
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr ~= nil then
		if plr.PlayerGui:FindFirstChild("PvPGui") ~= nil then return end
		if plr:FindFirstChild("_pvp") == nil then return end
		local ScreenGui = Instance.new("ScreenGui")
		ScreenGui.Name = "PvPGui"
		local Main = Instance.new("Frame")
		local Message = Instance.new("TextLabel")
		ScreenGui.Parent = plr.PlayerGui
		ScreenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
		Main.Name = "Main"
		Main.Parent = ScreenGui
		Main.BackgroundColor3 = (plr["_pvp"].Value == true) and Color3.fromRGB(255, 0, 0) or Color3.fromRGB(0, 255, 0)
		Main.BorderSizePixel = 0
		Main.Position = UDim2.new(0.5, 0, 0.718999982, 0)
		Main.Size = UDim2.new(0.00262173568, 0, 0.100000009, 0)
		Message.Name = "Message"
		Message.Parent = Main
		Message.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
		Message.BorderSizePixel = 0
		Message.Position = UDim2.new(0.0156284887, 0, 0, 0)
		Message.Size = UDim2.new(0.984371483, 0, 1, 0)
		Message.Font = Enum.Font.Highway
		Message.Text = (plr["_pvp"].Value == true) and "PvP Is Enabled." or "PvP Is Disabled."
		Message.TextColor3 = (plr["_pvp"].Value == true) and Color3.fromRGB(167, 0, 0) or Color3.fromRGB(0, 167, 0)
		Message.TextScaled = true
		Message.TextSize = 14.000
		Message.TextWrapped = true
		local function JRYXR_fake_script()
			local script = Instance.new('LocalScript', ScreenGui)
			local OriginalPos = script.Parent.Main.Position
			local goal = {}
			goal.Position = UDim2.new(0.38, 0, 0.719, 0)
			goal.Size = UDim2.new(0.24, 0, 0.1, 0)
			game:GetService("TweenService"):Create(script.Parent.Main, TweenInfo.new(3), goal):Play()
			wait(5)
			goal.Position = OriginalPos
			goal.Size = UDim2.new(0,0,0,0)
			game:GetService("TweenService"):Create(script.Parent.Main, TweenInfo.new(3), goal):Play()
			wait(3)
			script.Parent:Destroy()
		end
		coroutine.wrap(JRYXR_fake_script)()
	end
end)
2 Likes