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

Is there a way to make a wait() command without stopping the whole script?

Asked by 6 years ago

Code:

script.Parent.Derif.OnServerEvent:Connect(function(Player)

    local tweening = game:GetService("TweenService")
    local Tween = TweenInfo.new(
        7,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.Out,
        0,
        false,
        1)
    local partprop = {
        Size = Vector3.new(200,200,200);
        Transparency = 1
    }
local plr = Player.Character or Player.CharacterAdded:wait()
local hum = plr.Humanoid
local Folder = Instance.new("Folder", plr)
local explosion = Instance.new("Part", Folder)
local anim = tweening:Create(explosion,Tween,partprop)
explosion.Transparency = 0
explosion.CanCollide = false
explosion.Anchored = true
explosion.BrickColor = BrickColor.new("Gold")
explosion.CFrame = plr.HumanoidRootPart.CFrame
explosion.Shape = Enum.PartType.Ball
explosion.Size = Vector3.new(0.1,0.1,0.1)
explosion.BackSurface = "Smooth"
explosion.FrontSurface = "Smooth"
explosion.RightSurface = "Smooth"
explosion.LeftSurface = "Smooth"
explosion.TopSurface = "Smooth"
explosion.BottomSurface = "Smooth"
explosion.Parent = Folder
anim:Play()
hum.WalkSpeed = 0
wait(10)
hum.WalkSpeed = 16


explosion.Touched:Connect(function(hit)
 local inhum = hit.Parent.Humanoid
if inhum == nil then return end
if inhum.Parent == plr then
    --inhum.Health = inhum.Health - 0
    inhum.Health = inhum.Health - 10000
else
    inhum.Health = inhum.Health - 10000
end

end)
wait(3)
Folder:Destroy()
end)

In the code above, I tried to make the Player unable to walk for 10 seconds, but because I used wait() the whole script waited. And because of this, the explosion wouldn't kill the player immediately after touching it. How do I fix this?

0
put the Touched function above the wait GoldAngelInDisguise 297 — 6y
1
Indent your code theking48989987 2147 — 6y

1 answer

Log in to vote
3
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Yes, this can be used one of two ways, either the coroutine. method or the spawn(f) method. Preferably it's easiest to use the spawn method to have this work properly. The quick gist of this function is that it separates whichever nested lines into another thread allowing it to run separately from the mojo of the current running thread. Applying it is very simple, just write:

spawn(function()
    --// whatever code you wish to be unaffected remain outside of this encasing.
end)

Hope this helps! don't forget to accept and upvote:)

Still, don't understand? Read more about spawn here, and some quick pointers about threading

Stop by to check out coroutine too

0
This answer was really useful! Thanks! xXKingCool258Xx 39 — 6y
0
Glad to help! Ziffixture 6913 — 6y
Ad

Answer this question