Vehicle Spawning into Character vs into workspace

My vehicle is spawning into the character who regens it. However, this disables my scripts that allow it to be healed as well as damaged. What can I change here to make it so it doesn’t spawn into your character?


game.ServerStorage["Vehicle"]:remove()

local OriginColor = script.Parent.BrickColor

local JustRegened = false
local RegenWait = 5

Current = nil


local Allowance = 0 -- 0 = All, 1 = Group, 2 = GamePass.

local GroupId = 0
local GroupRank = 0
local GamePassId = 382612574

function CheckAllowed(plr)
	local ToReturn = false
	if Allowance == 0 then
		ToReturn = true
	elseif Allowance == 1 then
		if plr:IsInGroup(GroupId) and plr:GetRankInGroup(GroupId) >= GroupRank then
			ToReturn = true
		end
	elseif Allowance == 2 then
		if game:GetService("MarketplaceService"):PlayerOwnsAsset(plr, GamePassId) == true then
			ToReturn = true
		end
	end
	return ToReturn
end

function Click(plr)
	if JustRegened == false then
	JustRegened = true
	script.Parent.BrickColor = BrickColor.new("Really black")
	coroutine.resume(coroutine.create(function()
	if plr then
		if plr.Character then
			if CheckAllowed(plr) == true then
			coroutine.resume(coroutine.create(function()
			if plr.Character:findFirstChild("SpeederVehicle") ~= nil then
				plr.Character["SpeederVehicle"]:remove()
			end
			end))
			for i, v in pairs(game.Players:GetChildren()) do
				coroutine.resume(coroutine.create(function()
				if v then
					if v.Character then
						if v.Character:findFirstChild("SpeederVehicle") then
							if (v.Character["SpeederVehicle"]["Speeder"]["Engine"].Position - script.Parent.Position).magnitude <= 10 then
								v.Character["SpeederVehicle"]:remove()
							end
						end
					end
				end
				end))
				wait(0.01)
			end
			local NewCloneModel = Instance.new("Model", plr.Character)
			NewCloneModel.Name = "SpeederVehicle"
			local NewClone = Cloned:clone()
			--NewClone.Parent = plr.Character
			NewClone.Parent = NewCloneModel
			NewClone["Weld"].Disabled = false
			Current = NewClone
			end
		else
			if Allowance == 2 then
				game:GetService("MarketplaceService"):PromptPurchase(plr, GamePassId)
			end
		end
	end
	end))
	wait(RegenWait)
	JustRegened = false
	script.Parent.BrickColor = OriginColor
	end
end

script.Parent["ClickDetector"].MouseClick:connect(Click)
3 Likes

Clone it into the workspace instead of character. Plus don’t do:

local NewCloneModel = Instance.new("Model", plr.Character)
2 Likes

Is there a way I can replicate the exact model into the workspace? When I create a new model and then clone the old model into the workspace, it puts it into another model so then the repair tool doesn’t work. The code for that is:

local user
local player = game.Players.LocalPlayer
--when the tool is equipped
tool.Equipped:connect(function(mouse)
    --store the character of the person using the tool
    user = tool.Parent 
 
		if player.PlayerGui:FindFirstChild("Health") then
		else
		tool.Barrel.Health:Clone().Parent = player.PlayerGui
		end	


			
    --when the left mouse button is clicked
    mouse.Button1Down:connect(function()
        --make and do a hit test along the ray
        local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*20)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)
 		if hit == false then
		print("Fix")
		end

		local HealthGui = game.Players.LocalPlayer.PlayerGui.Health
		if HealthGui then
			repeat wait() until mouse.Target ~= nil
			if mouse.Target.Parent:FindFirstChild("Vehicle") then
				wait()
				HealthGui.Display.Text = ("Vehicle: "..mouse.Target.Parent:FindFirstChild("Vehicle").Value.." Health: "..mouse.Target.Parent.Health.Value)
		else 
		print("No")
		HealthGui.Display.Text = "Not Vehicle"
			end
		end	
			
		
		
        --do damage to any humanoids hit
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(50)
			a = hit.Parent:FindFirstChild("Torso")
			ab = Instance.new("Fire",a)
			ab.Parent = a
			

		local distance = (position - tool.Barrel.CFrame.p).magnitude
        local rayPart = Instance.new("Part", user)
		rayPart.Material = Enum.Material.Neon
		rayPart.Name          = "RayPart"
        rayPart.BrickColor    = BrickColor.new("Bright yellow")
        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.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)
		
 		tool.Barrel.PointLight.Enabled = true
		tool.Barrel.Flash.Flash.Visible = true
		tool.Barrel.Fire:Play()
		wait()
		tool.Barrel.PointLight.Enabled = false
		tool.Barrel.Flash.Flash.Visible = false
		game.Debris:AddItem(rayPart,.1)
		game.Debris:AddItem(ab,3)

			
		elseif hit.Parent:FindFirstChild("Health") then
		
			health = hit.Parent.MaxHealth.Value
			if hit.Parent.Health.Value + 10 <= health then
		hit.Parent.Health.Value = hit.Parent.Health.Value + 10
			else 
				hit.Parent.Health.Value = health	
			end	

		

        end
 
        --draw the ray
        local distance = (position - tool.Barrel.CFrame.p).magnitude
		local rayPart = Instance.new("Part", user)
		rayPart.Material      = Enum.Material.Neon
        rayPart.Name          = "RayPart"
        rayPart.BrickColor    = BrickColor.new("Bright yellow")
        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.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)
		
 		tool.Barrel.PointLight.Enabled = true
		tool.Barrel.Flash.Flash.Visible = true
		tool.Barrel.Fire:Play()
		wait()
		tool.Barrel.PointLight.Enabled = false
		tool.Barrel.Flash.Flash.Visible = false


        --add it to debris so it disappears after 0.1 seconds
        game.Debris:AddItem(rayPart, 0.1)

		end)
end)

As far as I can tell if the tool doesn’t find a humanoid as the mouse.Target.Parent’s first child, then it won’t count it as a vehicle. So if the vehicle is formatted like game.Workspace.SpeederVehicle.Vehicle:FindFirstChild("Humanoid") instead of game.Workspace.Vehicle:FindFirstChild("Humanoid") it will not work.

2 Likes

I think an easier way to tell which humanoids belong to a vehicle or an enemy human as I see in your script instead of doing an odd method like using a hierarchy dependent method as you explained above is to use the CollectionServices:

You can tag the humanoid instance with a string that says if it’s a vehicle and so don’t do the damage and even tag who owns the vehicle.

Edit: For example in your if statement

local humanoid = hit.Parent:FindFirstChild("Humanoid")

--using collection services
local  isEnemyHuman = CollectionServices:HasTag(humanoid,"BelongsToAEnemyHuman")
local  isVehicle = CollectionServices:HasTag(humanoid,"isVehicle")

if humanoid and isEnemyHuman then
--kill them lol
elseif humanoid and isVehicle then
--Heal the vehicle lol
end
2 Likes

Legendary coding at the end :raised_hands:. Thank you I will look into it because it looks a lot more hassle-free. Also which “if” statement are you looking at?