local tool = script.Parent local remoteEvent = tool:WaitForChild("ShootEvent")…
- User
local tool = script.Parent local remoteEvent = tool:WaitForChild("ShootEvent")
remoteEvent.OnServerEvent:Connect(function(player, mouseHit) local character = player.Character if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") if not humanoidRootPart then return end local bullet = Instance.new("Part") bullet.Name = "Bullet" bullet.Size = Vector3.new(0.2, 0.2, 0.2) bullet.Shape = Enum.PartType.Ball bullet.Material = Enum.Material.Neon bullet.BrickColor = BrickColor.new("Bright yellow") bullet.CanCollide = false bullet.CFrame = tool.Handle.CFrame bullet.Velocity = (mouseHit.Position - tool.Handle.Position).Unit * 300 bullet.Parent = workspace game:GetService("Debris"):AddItem(bullet, 5) bullet.Touched:Connect(function(hit) print("Пуля коснулась:", hit.Name) if hit and hit.Parent then if hit:IsDescendantOf(character) then print("Попали в себя, игнорируем.") return end local hitHumanoid = hit.Parent:FindFirstChild("Humanoid") if not hitHumanoid then hitHumanoid = hit.Parent.Parent and hit.Parent.Parent:FindFirstChild("Humanoid") end if hitHumanoid then print("Попали в Humanoid:", hitHumanoid.Parent.Name) hitHumanoid:TakeDamage(20) bullet:Destroy() else print("Humanoid не найден в:", hit.Parent.Name) end end end)end) сделай чтобы в этом скрипте когда оружие не было в руках то он не стрелял и чтобы пуля летела на 100 метров
- Assistant
To modify your script so that the weapon does not fire when it's not equipped (i.e., not in the player's hands) and to make the bullet fly exactly 100 meters, you can follow these steps:
-
Check if the weapon is equipped before firing:
- Use the
EquippedandUnequippedevents of the tool to track whether it's in the player's hands. - Introduce a boolean variable
equippedto keep track of the tool's state. - In the
OnServerEventfunction, check ifequippedistruebefore proceeding to fire.
- Use the
-
Limit the bullet's flight distance to 100 meters:
- Calculate the time it takes for the bullet to travel 100 meters based on its speed.
- Adjust the lifetime of the bullet accordingly using
Debris:AddItem.
Here's the updated script with the necessary modifications:
local tool = script.Parent local remoteEvent = tool:WaitForChild("ShootEvent") -- Variable to track if the tool is equipped local equipped = false -- Event handlers for when the tool is equipped and unequipped tool.Equipped:Connect(function() equipped = true end) tool.Unequipped:Connect(function() equipped = false end) remoteEvent.OnServerEvent:Connect(function(player, mouseHit) -- Check if the tool is equipped if not equipped then return end local character = player.Character if not character then return end local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") if not humanoidRootPart then return end -- Create the bullet local bullet = Instance.new("Part") bullet.Name = "Bullet" bullet.Size = Vector3.new(0.2, 0.2, 0.2) bullet.Shape = Enum.PartType.Ball bullet.Material = Enum.Material.Neon bullet.BrickColor = BrickColor.new("Bright yellow") bullet.CanCollide = false bullet.CFrame = tool.Handle.CFrame -- Calculate the direction and velocity local direction = (mouseHit.Position - tool.Handle.Position).Unit local bulletSpeed = 300 -- Units per second bullet.Velocity = direction * bulletSpeed bullet.Parent = workspace -- Calculate the time for the bullet to travel 100 meters local maxDistance = 100 -- 100 units local lifetime = maxDistance / bulletSpeed -- Time in seconds -- Set the bullet to be removed after it has traveled 100 meters game:GetService("Debris"):AddItem(bullet, lifetime) bullet.Touched:Connect(function(hit) print("Bullet touched:", hit.Name) if hit and hit.Parent then if hit:IsDescendantOf(character) then print("Hit self, ignoring.") return end local hitHumanoid = hit.Parent:FindFirstChild("Humanoid") if not hitHumanoid then hitHumanoid = hit.Parent.Parent and hit.Parent.Parent:FindFirstChild("Humanoid") end if hitHumanoid then print("Hit Humanoid:", hitHumanoid.Parent.Name) hitHumanoid:TakeDamage(20) bullet:Destroy() else print("No Humanoid found in:", hit.Parent.Name) end end end) end)Explanation of Changes:
-
Equipped Check:
- Added an
equippedvariable to track the tool's state. - Connected
tool.Equippedandtool.Unequippedevents to update theequippedvariable. - Before firing the bullet in
OnServerEvent, the script checks ifequippedistrue.
- Added an
-
Bullet Flight Distance:
- Calculated
lifetimeusing the formula:lifetime = maxDistance / bulletSpeed.- For 100 meters and a speed of 300 units/second:
lifetime = 100 / 300 = 0.333... seconds.
- For 100 meters and a speed of 300 units/second:
- Adjusted
Debris:AddItem(bullet, lifetime)to remove the bullet after it has traveled 100 meters.
- Calculated
Notes:
- The bullet's speed and maximum distance can be adjusted by changing the
bulletSpeedandmaxDistancevariables. - Ensure that the
ShootEventis properly handled on the client side to send themouseHitposition to the server. - It's important to handle cases where the tool might be unequipped before the bullet finishes its course.
This script now ensures that:
- The weapon only fires when it's equipped.
- The bullet flies exactly 100 meters before being removed from the game.
-