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
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
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
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)
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
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.
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)