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

How do you make lights different per player?

Asked by 4 years ago
Edited 4 years ago
local server = game:GetService("Workspace") 
server.Light:Clone().Parent = workspace.CurrentCamera
game.Workspace.Light:Destroy()

That code is on a local script and then I have a script as a child to a the light part that says this

local light = script.Parent.SpotLight
local sound = script.Parent.Sound
print("K")
light.Enabled = false
wait(8)
light.Enabled = false
sound:Play()
light.Enabled = true
wait(.5)
light.Enabled = false
wait(.1)
light.Enabled = true 
wait(1)
light.Enabled = false
wait(.2)
light.Enabled = true 
game.Workspace.Lights.Value = 1
sound:Stop()

this code in the part never runs for some reason. My end goal is to get this light thing to run for each player when they join the game. And only they can see the lights flashing.

1 answer

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

Is the script in the light part a local script? Local scripts don't run in workspace unless they are under the player's character. Here's a list of where local scripts can run:

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

  • A Player’s Backpack, such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts.
  • The ReplicatedFirst service

https://developer.roblox.com/en-us/api-reference/class/LocalScript

You have to place the local script in one of those areas for it to run.

If the script in the light part is not a local script, but is a normal Script, it still won't run because you're cloning the light part on the client, so the server Script will not be cloned along with the part. To fix this, you have to execute the code in a Local Script. What you can do is run the code inside the main Local Script as well, like so:

local server = game:GetService("Workspace") 
local lightClone = server.Light:Clone() -- store the light clone in a variable so that you can do stuff to it later on
lightClone.Parent = workspace.CurrentCamera
game.Workspace.Light:Destroy()

local light = lightClone.SpotLight -- instead of script.Parent.SpotLight
local sound = lightClone.Sound -- instead of script.Parent.Sound
print("K")
light.Enabled = false
wait(8)
light.Enabled = false
sound:Play()
light.Enabled = true
wait(.5)
light.Enabled = false
wait(.1)
light.Enabled = true 
wait(1)
light.Enabled = false
wait(.2)
light.Enabled = true 
game.Workspace.Lights.Value = 1
sound:Stop()

Keep in mind that this means that game.Workspace.Lights.Value is only set to 1 for the client, and not everyone else. The same goes for playing and stopping the sound, the sound will only play and stop for the client who has this local script.

0
Thank you! techN0logics 40 — 4y
0
It's exactly what I wanted it to do, only play for the client who has the script so it worked amazingly well. techN0logics 40 — 4y
Ad

Answer this question