Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The script works for a while, but then "freezes"? [SOLVED]

Asked by 6 years ago
Edited 6 years ago

Hello there! Allow me to explain what I mean by it "freezes". :P (Not quite sure how to word the question title.)

What occurs is that for some reason, while the script's running, it all of a sudden "stops". Diagnosing the problem, I couldn't figure out what was wrong; I applied prints, but they all printed. Pretty weird. <->

What I'm trying to accomplish is getting a model to float, and then go back to its original position; however, after a few executions, it goes back to its original position, then stays there motionless for some reason, just "staring" blankly off into the distance.

What makes things worse is that there weren't any errors. :c

Anybody know what the problem is? This's the script in all of its glory:

local primarypart = script.Parent.PrimaryPart
local originalpos = script.Parent:GetPrimaryPartCFrame() -- Short for `original position`

local bv = Instance.new('BodyVelocity')
bv.MaxForce = Vector3.new(12e2+50, 12e2+50, 12e2+50)
bv.P = 12e2+50
bv.Velocity = Vector3.new(0,0,0)
bv.Parent = primarypart

local bg = Instance.new('BodyGyro')
bg.MaxTorque = Vector3.new(5e4, 5e4, 5e4)
bg.P = 5e4
bg.CFrame = originalpos
bg.Parent = primarypart

function returnprimarycframe()
    return script.Parent:GetPrimaryPartCFrame()
end

function getclosest()
    local datplr
    local thedistance = 250
    for i, v in next, game:GetService('Players'):GetPlayers() do
        if v.Character then
            local distance = (returnprimarycframe().p-v.Character:GetModelCFrame().p).Magnitude
            if distance < thedistance then
                thedistance = distance
                datplr = v.Character:GetModelCFrame().p
            end
        end
    end
    return datplr
end

spawn(function() --v This also stops for some reason; it's supposed to have the model face the player.
    while true do
        wait(1)
        local aplr = getclosest()
        if aplr then
            bg.CFrame = CFrame.new(returnprimarycframe().p, aplr)
        end
    end
end)

while true do --v The floating part, which mysteriously stops working.
    bv.Velocity = Vector3.new(0,5,0)
    wait(2)
    bv.Velocity = Vector3.new(0,0,0)
    wait(2)
    pcall(function() -- I applied this just in case it was problem, but it was later removed; I'm keeping it here for the sake of what I tried doing to fix it. :P
        script.Parent:SetPrimaryPartCFrame(originalpos)
    end)
    wait(2)
end

Tyvm in advance! ^^

EDIT

So far, I went back in and applied print to see what's going on on the server, and in studio. Funny enough, the script was still running, but it wasn't floating, and stare off again! :O

Updated script:

local primarypart = script.Parent.PrimaryPart
local originalpos = script.Parent:GetPrimaryPartCFrame()

local bv = Instance.new('BodyVelocity')
bv.MaxForce = Vector3.new(12e2+50, 12e2+50, 12e2+50)
bv.P = 12e2+50
bv.Velocity = Vector3.new(0,0,0)
bv.Parent = primarypart

local bg = Instance.new('BodyGyro')
bg.MaxTorque = Vector3.new(5e4, 5e4, 5e4)
bg.P = 5e4
bg.CFrame = originalpos
bg.Parent = primarypart

function returnprimarycframe()
    print('return cframe') -- Prints
    return script.Parent:GetPrimaryPartCFrame()
end

function getclosest()
    print('get the closest') -- Prints
    local datplr
    local thedistance = 250
    for i, v in next, game:GetService('Players'):GetPlayers() do
        if v.Character then
            local distance = (returnprimarycframe().p-v.Character:GetModelCFrame().p).Magnitude
            if distance < thedistance then
                print('found') -- Prints
                thedistance = distance
                datplr = v.Character:GetModelCFrame().p
            end
        end
    end
    return datplr
end

spawn(function()
    while true do
        wait(1)
        local aplr = getclosest()
        print('plr:', aplr) -- Prints
        if aplr then
            print('plr found') -- Prints
            bg.CFrame = CFrame.new(returnprimarycframe().p, aplr)
        end
    end
end)

while true do
    bv.Velocity = Vector3.new(0,5,0)
    print('hai') -- Prints
    wait(2)
    bv.Velocity = Vector3.new(0,0,0)
    print('how') -- Prints
    wait(2)
    script.Parent:SetPrimaryPartCFrame(originalpos)
    print('u duin?') -- Prints
    wait(2)
end

Interesting, yes?

EDIT2

Turns out the problem was another script, meant to anchor and lock parts. Forgot about it. XP

Tyvm for your response! ^^

0
Lines 52 snd 54, you have an end) inside of an end. Big no no. User#19524 175 — 6y
0
^ What does that have to do with the script "freezing"? TheeDeathCaster 2368 — 6y
1
^ Litteraly nothing hunterthegreat100 18 — 6y
0
^ TheeDeathCaster 2368 — 6y
View all comments (3 more)
0
Make sure that all while true do loops have wait()'s. DeceptiveCaster 3761 — 6y
0
They all have waits though. TheeDeathCaster 2368 — 6y
0
And the cycle begins again! :D TheeDeathCaster 2368 — 6y

Answer this question