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

Disabling Shake Script After Certain Amount of time?

Asked by 4 years ago

So basically, I'm trying to disable a shake script after 3 seconds for an Order 66. The ShakeScript is a while true function, and I'm trying to get it to end after 3 seconds.


local players = game.Players:GetPlayers() for i=1, #players do local character = players[i].Character local shakescript = character:FindFirstChild("ShakeScript") if shakescript ~= nil then shakescript.Disabled = false end end game.Workspace.ShakeScriptAdder.ShakeScript.Disabled=false wait(3) game.Workspace.ShakescriptAdder.ShakeScript.Disabled = true local players = game.Players:GetPlayers() for i=1, #players do local character = players[i].Character local shakescript = character:FindFirstChild("ShakeScript") if shakescript ~= nil then shakescript.Disabled = true end end

This is the Shake Script if you need it.


while true do local cam = workspace.CurrentCamera cam.CFrame=cam.CFrame*CFrame.Angles(math.random(-3,3)/360,math.random(-3,3)/360,math.random(-3,3)/360) cam.FieldOfView=math.random(69,71) wait() end

(There is another script that just distributes the ShakeScripts into each player. It works, it just wont stop.)

2 answers

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago

Disabling scripts during a gameplay is quite unreliable. While i had some use for enabling disabled scripts, other way round does not seem to do much. You will need a completely different approach. I will amend your scripts to use FireAllClients() function.

First lets edit your local "ShakeScript". For my solution to work, please set it to Enabled (un-check Disabled checkbox) before distributing to clients. We will get rid of infinite loop, you should avoid them, whenever you can.

ReplicatedStorage = game:GetService("ReplicatedStorage")
ShakeEvent = ReplicatedStorage:WaitForChild("ShakeEvent")

local function OnShakeEvent(duration)
    for i = 0, 30*duration do --wait() is about 1/30 of second long
        local cam = workspace.CurrentCamera
        cam.CFrame=cam.CFrame*CFrame.Angles(math.random(-3,3)/360,math.random(-3,3)/360,math.random(-3,3)/360)
        cam.FieldOfView=math.random(69,71)
        wait()
    end
end
ShakeEvent.OnClientEvent:Connect(OnShakeEvent)

Now your server script. Now you can adjust duration of the shake, or even fire client separately with FireClient(player) event.

ReplicatedStorage = game:GetService("ReplicatedStorage")
ShakeEvent = Instance.new("RemoteEvent")
ShakeEvent.Name = "ShakeEvent"
ShakeEvent.Parent = ReplicatedStorage

SHAKE_DURATION = 3 --that's in seconds
ShakeEvent:FireAllClients(SHAKE_DURATION)
0
Oh god wow, it worked. Thanks! I'll try to remember that next time. youngsavagehard 20 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

To start off, I would use a Remote Event and I suggest reading more about them and Remote Functions on the ROBLOX developer wiki as they are very useful since all games need to be filtering enabled. Also, I suggest not directly disabling the script itself, just disabling/enabling all the things you want the script to do.

This is what you would write for the client(local script):

local RepStorage = game:GetService("ReplicatedStorage")
local shakerEvent = RepStorage:WaitForChild("shakeEvent")

local function shaker()
--write what you want the event to do (an example would be workspace.Part.Transparency = 0)
end

shakerEvent.OnClientEvent:connect(shaker)

This is what you would write in the server script:

local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")
local shakeRemote = Instance.new("RemoteEvent")
shakeRemote.Parent = RepStorage
shakeRemote.Name = "shakeEvent"

local function shakeShake()
-- depending on whether you want the event to fire for all players when fired or only for the player firing it, What you would write for the event to fire for all players is:
    shakeRemote:FireAllClients()
end

-- if you want the event to fire only on the player firing it, you would write:
local function shakeShake(player)
    shakeRemote:FireClient(player)

--once you want the event to fire, you would put it wherever you want it to fire in the server script as:
shakeShake()

Hopefully this helped! any further questions you can message me on discord, Child#3611 If this did work, make sure you accept my answer as solved :D

Answer this question