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

My Transparency loop doesnt lower transparency?

Asked by 2 years ago

The script works for the most part, except the transparency doesnt lower at all when the loop is activated? any tips would be nice

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local smoke = ReplicatedStorage.ParticleSmoke.ParticleEmitter



local function BarrelShift()
    local ClonedS = smoke:Clone()
    ClonedS.Parent = player.Character:FindFirstChild("HumanoidRootPart")

    while ClonedS.Transparency.Keypoints[1].Value >= 0.1 do
        ClonedS.Transparency = NumberSequence.new(ClonedS.Transparency.Keypoints[1].Value - 1)
        wait(0.1)
    end
    wait(1.3)
    ClonedS:Destroy()
end

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.C then
        BarrelShift()
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Thats because you are being a bozo. If thats not the whole problem and it still doesnt work, the following is 50% of the problem. Its called a logic error. You are looping in a function that is expected to be called mutiple times.

A Thread is a single sequential flow of control within a program. When you loop, you dont execute code after the loop immediately. Either the loop has to end and THEN everything else can continue OR you run the loop in a DIFFERENT thread so that the loop runs AND everything else such as the inputservice code can function simultaniously. Dont create too many threads though, because it still uses the same processor(s), it just alternates between code in all threads so it SEEMS like its simultanious, but its not, everything is slower. Its still really fast because electricity is like that but too many threads will cause noticable slowdown of code execution from all threads.

to DO THIS you can use spawn or coroutine. Coroutines are said to be more reliable so do this.

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local smoke = ReplicatedStorage.ParticleSmoke.ParticleEmitter



local function BarrelShift()
    local ClonedS = smoke:Clone()
    ClonedS.Parent = player.Character:FindFirstChild("HumanoidRootPart")

    while ClonedS.Transparency.Keypoints[1].Value >= 0.1 do
        ClonedS.Transparency = NumberSequence.new(ClonedS.Transparency.Keypoints[1].Value - 1)
        wait(0.1)
    end
    wait(1.3)
    ClonedS:Destroy()
end

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.C then

        coroutine.wrap(function() -- this line creates a new thread. Wrap just creates and runs whatever is in it in a new thread.

            BarrelShift() -- executes function in new thread

        end)()

    end
end)

This code, unless theres another issue, should run the function every time C is pressed. Before this change, in your original code, since the loop was running, it couldnt return the function because that loop in the function was still running, which made it so the line the interpreter was stuck on was

BarrelShift()

and it couldnt run it again even if C is pressed again during that time. But now with my new and improved version of your code, it runs that function in a separate thread meaning the function will return and that line will be over quickly and userinput should function again.

Ad

Answer this question