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

How do you assign a gun to the tool?

So, I am trying to integrate a tool containing a gun script with a vehicle. The problem is, I cant assign the tool to the specific gun. Line 27 is where the bullet exits out of. But since there is more than 1 vehicle, the bullets will only spawn at that gun. Here is the gun script:

enabled = false
player = game:GetService("Players").LocalPlayer                                       
tool = script.Parent
bulletDamage = 350
on = false
mouse = player:GetMouse()
local gunIsFiring = false
local function Shoot()
	if enabled == false then
		gunIsFiring = true
			local sound = Instance.new("Sound",workspace)
        sound.SoundId = "rbxassetid://136523485"
        sound:Play()
game.Debris:AddItem(sound, 2)
			local bulletProjectile = Instance.new("Part", workspace)
			bulletProjectile.Shape = ("Ball")
			bulletProjectile.Name = ("Bullet")
			game.Debris:AddItem(bulletProjectile, 5)
			bulletProjectile.Size = Vector3.new(0.4, 0.4, 0.4)
			local bulletProjectileM = Instance.new("SpecialMesh", bulletProjectile)
			bulletProjectileM.MeshType = "Sphere"
			bulletProjectileM.Scale = Vector3.new(1, 1, 1)
			bulletProjectile.BrickColor = BrickColor.new("Black")
			bulletProjectile.Reflectance = 0.5
			bulletProjectile.TopSurface = "Smooth"
			bulletProjectile.BottomSurface = "Smooth"
			bulletProjectile.CFrame = game.Workspace.Coolcar.HoverV.Hole.CFrame * CFrame.new(0,0,1)
			bulletProjectile.CFrame = CFrame.new(bulletProjectile.Position, mouse.hit.p)
			local bulletForce = Instance.new("BodyVelocity", bulletProjectile)
			bulletForce.velocity = bulletProjectile.CFrame.LookVector * 360
			bulletForce.maxForce = Vector3.new(90000, 90000, 90000)
			game.Debris:AddItem(bulletProjectile, 3)
				local function harmPart(Part)
					
					
local splode = Instance.new("Explosion", game.Workspace)
splode.ExplosionType = "NoCraters"
splode.DestroyJointRadiusPercent = 1
splode.BlastPressure = 10
splode.BlastRadius = 5
splode.Position = bulletProjectile.Position
game.Debris:AddItem(splode, 0.1)
if Part.Parent:FindFirstChild("Humanoid") ~= nil then
Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health -(bulletDamage)
end
			end
			bulletProjectile.Touched:connect(harmPart)
			enabled = true
			wait(0.5)
			enabled = false
			end
			
		end
	
	
tool.Equipped:connect(function(Mouse)
Mouse.Button1Down:connect(Shoot)
		
end)
	

tool.Unequipped:connect(function(Mouse)
end)

1 comment

0

If the script is inside the vehicle/gun, you can do "script.Parent" and so on to get to where you want.

1 answer

✓ Accepted answer

you have set the bullet CFrame to a specific object in line 27

bulletProjectile.CFrame = game.Workspace.Coolcar.HoverV.Hole.CFrame + ....

the bullet will always fly out from the object. also you cannot use script.Parent in this case, because the tool already belongs to your player. it lost all the information where it is copied from.

the only solution you have is, when the script(or the tool of the script) is copied to the player. The copier script(when you sit on the seat) create an object value inside the tool, assign the gun object to that object value. so when the tool shows up in the backpack, it remembers which gun the seat and the player is associated to. Because there is an object value inside the tool and the object value knows the gun.

2 comments

0

I will try that

0

I will accept your answer if it works

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