Any reason this might freeze my clients?

This seems to randomly freeze my clients when the tool is activated for some reason. Not sure why. I can’t figure out a way to reproduce it other than sometimes when my tool is activated it freezes.

This is the code for a bow. I’m not sure if using egomoose’s custom mouse module might be causing the freezing? I will give more info if needed!

Help appreciated!

    script.Parent.Activated:connect(function()

    if db == false then

    db = true

    hold:Play()

    local ray = Ray.new(script.Parent.Handle.Position, mouse.Hit.p)

    local hit, pos = game.Workspace:FindPartOnRay(ray)

    print(hit)

    if hit then--]]

    script.Parent.Fire:FireServer(mouse.Hit)

    end

    local distance = (script.Parent.Handle.CFrame.p - mouse.Hit.p).magnitude

    local p = Instance.new("Part")

    p.Name = "ClientBowEffect"

    p.Size = Vector3.new(0.1, 0.1, distance)

    p.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, mouse.Hit.p) * CFrame.new(0, 0, -distance / 2)

    p.BrickColor = BrickColor.new("White")

    p.Transparency = .5

    p.CanCollide = false

    p.Anchored = true

    game:GetService("Debris"):AddItem(p, 1)

    p.Parent = game.Workspace

    ts:Create(p, TweenInfo.new(.5), goal):Play()

    wait(1)

    hold:Stop()

    wait(2)

    db = false

    end

    end)

First thing first, you should probably fix your formatting. Your code is hard to follow, and the double newlines just make it harder to read. Generally the best way of formatting I know of is organising things visually. This means using indendations to organise blocks of code, and lines of code with similar purposes being grouped together. How you format your code quite subjective and is ultimately up to you, however this method is the most common and should probably be what you roughly follow when sharing code with other people (for the purposes of making it easy to read for them :wink: ). Here’s your version with fixed formatting following that general format:

script.Parent.Activated:connect(function()
    if db == false then
        db = true
        hold:Play()

        local ray = Ray.new(script.Parent.Handle.Position, mouse.Hit.p)
        local hit, pos = game.Workspace:FindPartOnRay(ray)
        print(hit)

        if hit then--]]
            script.Parent.Fire:FireServer(mouse.Hit)
        end

        local distance = (script.Parent.Handle.CFrame.p - mouse.Hit.p).magnitude
       
        local p = Instance.new("Part")
        p.Name = "ClientBowEffect"
        p.Size = Vector3.new(0.1, 0.1, distance)
        p.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, mouse.Hit.p) * CFrame.new(0, 0, -distance / 2)
        p.BrickColor = BrickColor.new("White")
        p.Transparency = .5
        p.CanCollide = false
        p.Anchored = true
        p.Parent = game.Workspace

        game:GetService("Debris"):AddItem(p, 1)
        ts:Create(p, TweenInfo.new(.5), goal):Play()

        wait(1)
        hold:Stop()
        wait(2)
        db = false
    end
end)

Now as for fixing your code, I don’t immediately see any issues. However, your best bet when debugging is to just remove bits of code (comments would be best for this) and see whether it still freezes. So have a go at that and tell us how it goes.

What do you mean by ‘sometimes’? It sometimes freezes when you activate it, and sometimes doesn’t?

4 Likes