Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

local tool = script.Parent local remoteEvent = tool:WaitForChild("ShootEvent")…

  1. 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 метров

  2. 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:

    1. Check if the weapon is equipped before firing:

      • Use the Equipped and Unequipped events of the tool to track whether it's in the player's hands.
      • Introduce a boolean variable equipped to keep track of the tool's state.
      • In the OnServerEvent function, check if equipped is true before proceeding to fire.
    2. 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 equipped variable to track the tool's state.
      • Connected tool.Equipped and tool.Unequipped events to update the equipped variable.
      • Before firing the bullet in OnServerEvent, the script checks if equipped is true.
    • Bullet Flight Distance:

      • Calculated lifetime using the formula: lifetime = maxDistance / bulletSpeed.
        • For 100 meters and a speed of 300 units/second: lifetime = 100 / 300 = 0.333... seconds.
      • Adjusted Debris:AddItem(bullet, lifetime) to remove the bullet after it has traveled 100 meters.

    Notes:

    • The bullet's speed and maximum distance can be adjusted by changing the bulletSpeed and maxDistance variables.
    • Ensure that the ShootEvent is properly handled on the client side to send the mouseHit position 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.