I get the error "TeamColor is not a valid member of model" whenever I hit someone. I'm not sure why it does this, because it checks for player and player isn't their character, unless i'm wrong.
Also, lines 23-27 is where the problem is.
local tool = script.Parent
local reloadTime = 1;
local reloading = false;
tool.Equipped:connect(function(mouse)
local user = tool.Parent
local plr = game.Players.LocalPlayer
mouse.TargetFilter = user
mouse.Button1Down:connect(function()
if reloading then
return
end
if user:FindFirstChild("Humanoid") and user.Humanoid.Health > 0 then
reloading = true
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if humanoid and player.TeamColor ~= user.TeamColor then
humanoid:TakeDamage(15)
end
local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", workspace)
rayPart.Name = "RayPart"
rayPart.BrickColor = plr.TeamColor
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance / 2)
game.Debris:AddItem(rayPart, 0.1)
local start_t = tick()
while game:GetService("RunService").RenderStepped:wait() do
if tick() - start_t() > reloadTime then
break
end
end
reloading = false
end
end)
end)

3 comments
How would I fix this in my code?
Reference his links.
you have to figure out how to fix it, find out what line isn't working, and add the GetPlayerFromCharacter() method so that it no longer is referencing the model but rather the local player of that model