So I've been trying to work on a script that deletes all the pointlights in a game if you click and hold a certain brick for about 9 seconds. I've been having trouble with having them delete and then come back after 1 minute and 30 seconds. How can I do this? Everything I seem to try to do is not working.
Can you explain how to do this using simple bricks with pointlights and having them delete if you press and hold another brick. Thank you :)
Well, i don't know EXACTLY how your final script will be like (because i don't know how you can detect holding a brick with mouse). But i can give you an idea:
While the brick is being held, a BoolValue
somewhere both server and local can access gets set to true
.
A server script, using the Changed
event of the BoolValue
detects wether it is set to true
or not and if true
it moves all PointLights
to ServerStorage
(or somewhere a Part
can be placed inside but without getting rendered or altered).
After the brick is let go, the LocalScript
(or some other script, if you'd prefer less local load) will set said BoolValue
to false
. The server script will then detect that it's false
and restore all PointLights
back to Workspace
, exactly where they were left before.
If you wanted to delete, i thought of something, but it'd be less effecient script-wise. The way i told you right now is much simplier to do, as a Part
placed inside Replicated/ServerStorage
essensially functions as if you had used the Remove()
function on it, except all scripts can recall the Part
anytime.
Bonus Note:
For extra effeciency, put the PointLights inside a Folder
, so instead of searching all the Workspace
for the PointLights
, the children of said Folder
just get easily picked up!
Make sure to have a Folder
of the same name in the destination, for more effeciency.
So it looks like this:
Part hold boolean
local lights = game.ServerStorage.Lights:GetChildren() --(UserInputService thing in LocalScript/Server Script) repeat wait() until #lights == 0 --Wait for lights to exist in Workspace game.ReplicatedStorage.Held.Value = true --(Brick is not held anymore) game.ReplicatedStorage.Held.Value = false end
Server script handling PointLights
game.ReplicatedStorage.Held.Changed:connect(function(bool) if bool == true then for _,part in pairs(workspace.Lights:GetChildren()) do part.Parent = game.ServerStorage.Lights end else wait(3) --Delay for returning for _,part in pairs(game.ServerStorage.Lights:GetChildren()) do part.Parent = workspace.Lights end end end)
Here you go! That's just how it should work!
Once you add the way the BoolValue
gets triggered, no problem afterwards!
EDIT - Added precisely what asker was asking for, made overall answer fancier