BUG with RemoteEvents

I don’t really know why it’s doing this (creating a second Instance.new("Part")), there’s also an error which I don’t think is bothering me that much, but I still want there to be no errors.

Script :

local Tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local RE = RS.Gun
local Active = true

RE.OnServerEvent:Connect(function(Player, Mouse)
	local PartPos = Tool.Part.Position
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(1, 1, 1)
	Part.CanCollide = false
	Part.CanQuery = false
	Part.CFrame = CFrame.new(PartPos, Mouse)
	Part.Position = PartPos
	Part.Parent= workspace.TrashCan
	
	Part.Velocity = Part.CFrame.LookVector * 300
	
	Part.Touched:Connect(function(Hit)
		if Hit then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			local Team = Player.Team
			
			if Hit.Parent ~= Tool.Parent and game.Players:GetPlayerFromCharacter(Humanoid.Parent).Team ~= Player.Team and Active then
				Active = false
				Humanoid.Health = Humanoid.Health - 25.1
				task.wait(0.1)
				Active = true
			end
		end
	end)
end)

The error I’m getting :

attempt to index nil with 'Parent' - Server - Script:23

2 Likes

You’d have to check if there is a humanoid

Part.Touched:Connect(function(Hit)
		if Hit then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			local Team = Player.Team
			
			if Humanoid then
				if Hit.Parent ~= Tool.Parent and game.Players:GetPlayerFromCharacter(Humanoid.Parent).Team ~= Player.Team and Active then
					Active = false
					Humanoid.Health = Humanoid.Health - 25.1
					task.wait(0.1)
					Active = true
				end
			end
		end
2 Likes

Oh yeah, I somehow deleted the If Humanoid, the creating a second Instance.new("Part") is still happening though, It only happens when there’s 2 players in the game

1 Like

Video :

2 Likes

Part.Touched only fire when there’s a Hit, you don’t need to check for a Hit, otherwise it wouldn’t run/activate in the first place.
If you want to detect players and npcs, I recommend checking for humanoid first.
However, if you want to check only for players, I would instead recommend to check if it’s a player, that way npcs or other objects with a humanoid won’t do false positives.

Part.Touched:Connect(function(Hit)
		local EnemyPlayer = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if EnemyPlayer then
			local Humanoid = EnemyPlayer:FindFirstChild("Humanoid")
			
			if EnemyPlayer ~= Player and EnemyPlayer.Team ~= Player.Team and Active then
				Active = false
				Humanoid.Health = Humanoid.Health - 25.1
				task.wait(0.1)
				Active = true
			end
		end
	end)
1 Like

why is it getting the humanoid.parent that leads to workspace
or its correct? idk

The attempt to index nil with 'Parent' - Server - Script:23 has already been solved by @yoshicoolTV , but I’m still getting a duplicate of a new part everytime the tool is activated

Humanoid.Parent is the character though

It looks like maybe the 2nd player’s weapon is being fired from the location it is being stored in.
Maybe as the very first thing in your OnServerEvent, check if it’s the correct player that’s firing it as well that it’s equipped by the player.

RE.OnServerEvent:Connect(function(Player, Mouse)
	local ToolPlayer = game.Players:GetPlayerFromCharacter(Tool.Parent)
	if Player ~= ToolPlayer then return end

This prevent the code from running, if it’s not the same player who activated the remote, as the one having the tool equipped.

1 Like

Setup your “Active” somewhere else (more at the beginning)

local Tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local RE = RS.Gun
local Active = true

RE.OnServerEvent:Connect(function(Player, Mouse)
	if not Active then return end
	local PartPos = Tool.Part.Position
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(1, 1, 1)
	Part.CanCollide = false
	Part.CanQuery = false
	Part.CFrame = CFrame.new(PartPos, Mouse)
	Part.Position = PartPos
	Part.Parent= workspace.TrashCan
	
	Part.Velocity = Part.CFrame.LookVector * 300
	
	Part.Touched:Connect(function(Hit)
		if Hit then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			local Team = Player.Team
			
			if Hit.Parent ~= Tool.Parent and game.Players:GetPlayerFromCharacter(Humanoid.Parent).Team ~= Player.Team and Active then
				Active = false
				Humanoid.Health = Humanoid.Health - 25.1
				task.wait(0.1)
				Active = true
			end
		end
	end)
end)
1 Like

I understand, it was merely a suggestion on how the code could be improved.

2 Likes

Thank you very much, that solved my problem tysm!

2 Likes

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