Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problem with my laser gun code? [UN-ANSWERED]

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)

1 answer

✓ Accepted answer

TeamColor is not a part of the player's model, but rather a property in the player object under game.Players, or more commonly known as LocalPlayer. Use the GetPlayerFromCharacter(char) method to get the player object from game.Players using the character model in workspace which is what you are hitting. Then you can compare the teamcolors using the local player of that character you are hitting.

LINKS:

(http://wiki.roblox.com/index.phptitle=API:Class/Players/GetPlayerFromCharacter) (http://wiki.roblox.com/index.php?title=API:Class/Player/TeamColor)

3 comments

0

How would I fix this in my code?

0

Reference his links.

0

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

This discussion is preserved as it was. New answers, comments, and votes are closed.