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

Why does this script stop?

Asked by 9 years ago
function onTouch(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        screengui = Instance.new("ScreenGui", player.PlayerGui)
        frame = Instance.new("Frame", screengui)
        frame.Size = UDim2.new(1, 0, 1, 0)
        frame.BackgroundColor3 = Color3.new(255, 255, 255)
        frame.BackgroundTransparency = 1
        for i = 1, 10 do
            frame.BackgroundTransparency = frame.BackgroundTransparency - 0.1
            wait()
        end
        hit.Parent.Torso.CFrame = workspace.BedSpawn.CFrame
        wait(0.5)
        for i = 1, 10 do
            frame.BackgroundTransparency = frame.BackgroundTransparency + 0.1
            wait()
        end
        screengui:Destroy()
    end
end

script.Parent.Touched:connect(onTouch)

This script makes the screen flash when a player hits an object and teleports the player to another location, then fades out. The problem is, when the gui fades out and hits around 0.8 transparency, it just stops. The gui doesn't get destroyed. How come?

1 answer

Log in to vote
1
Answered by
Scubadoo2 115
9 years ago

The problem is not in the script, but how it behaves. It is doing exactly what you want it to do, but, when it gets hit again the variables screengui and frame get replaced. The reason why it seems to stop at .8 is that it is probably when your other foot hit the brick and it "dumps" the variables.

The best fix for this is to make them local like so:

local screengui = Instance.new("ScreenGui", player.PlayerGui)
local frame = Instance.new("Frame", screengui)

By keeping them local to when the function is called, they won't get "dumped" and will act as intended.

Did I explain that well, or did I confuse you?

0
It works! Thanks! IcyArticunoX 355 — 9y
0
You're welcome. Did you understand what I said? Scubadoo2 115 — 9y
Ad

Answer this question