Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detecting if hit part has can collide off?

Asked by 4 years ago

Hi, i'm trying to add to the code below that if Hit has can collide set as false it doesn't change the value like "not game.Players:GetPlayerFromCharacter(Hit.Parent)" already achieves. But i believe im going about it the wrong way and would appreciate if someone could lead me the correct way!

This is the line i'm trying to add it to, so basically if the script.Parent is hit, i want it so if the part hit has can collide turned off it doesn't change the Health value.

if Speed > 30 and not game.Players:GetPlayerFromCharacter(Hit.Parent) and Hit.CanCollide == false then

The full code is below.

script.Parent.Touched:Connect(function(Hit)
local Speed = math.floor((script.Parent.Velocity - Hit.Velocity).magnitude)

if Speed > 30 and not game.Players:GetPlayerFromCharacter(Hit.Parent) and Hit.CanCollide == false then
     script.Parent.Health.Value = script.Parent.Health.Value - script.Parent.Velocity.Magnitude / 1.06
  print("You screwed your car up m8. Speed: "..Speed)
  local RandomNumber = math.random(1, 2)

  if RandomNumber == 1 then
    local CollideSound1 = Instance.new("Sound", script.Parent)
    CollideSound1.SoundId = "rbxassetid://490314550"
    CollideSound1:Play()
  else

    local CollideSound2 = Instance.new("Sound", script.Parent)
    CollideSound2.SoundId = "rbxassetid://215550851"
    CollideSound2:Play()
  end

  if script.Parent.Health.Value <= 0 then
    script.Parent.Smoke.Enabled = true
  end

if script.Parent.Health.Value <= 0 then
        script.Parent.Parent.Parent.Parent.Parent.DriveSeat.Disabled = true
script.Parent.Parent.Repair.BillboardGui.Enabled = true

end

elseif Speed < 30 and not game.Players:GetPlayerFromCharacter(Hit.Parent) then
  print("Keep tryin. Speed: "..Speed)
end
end)

1 answer

Log in to vote
0
Answered by 4 years ago
--1.Option: You don't need to use game.Players:GetPlayerFromCharacter function, you need to get Humanoid first.
if Hit.Parent:FindFirstChild("Humanoid") then
if Speed > 30 and not game.Players:GetPlayerFromCharacter(Hit.Parent) and Hit.CanCollide == false then

--2.Option: You can use only the Humanoid
if Speed > 30 and not Hit.Parent:FindFirstChild("Humanoid") and Hit.CanCollide == false then --WARNING: This option can not pass the NPC's too.
Ad

Answer this question