Hello, i was trying to make a flashlight script lately and i watched some tutorials and i managed to find a code from one guy that works, but the issue is that only I can see the light and not other players on the server. I know i need to use remote events to communicate with server but the script includes RenderStepped function and i cannot manage to make it on the server. Any help will be appreciated!
Here is the LocalScript:
local cam = workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Flashlight = RS.Flashlight
local Clone = Flashlight:Clone()
Clone.Parent = script.Parent
local Brightness = 1
local Keybind = Enum.KeyCode.F
local UIS = game:GetService("UserInputService")
local Toggle = false
local Mouse = game.Players.LocalPlayer:GetMouse()
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)
local ClickSFX = workspace.SFX.Flashlight
UIS.InputBegan:Connect(function(Input, p)
if p then return end
if Input.KeyCode == Keybind then
Toggle = not Toggle
ClickSFX:Play()
end
end)
RunService.RenderStepped:Connect(function()
if Clone then
Clone.Position = cam.CFrame.Position
TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, Mouse.Hit.Position)}):Play()
if Toggle then
TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
else
TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
end
end
end)
well, since the flashlight is activated via local script, it is only seen locally and cannot be seen by others, maybe try to make another solution that works on the server?
So i need to make an animation that will turn on the flashlight’s light or just an animation that will only move character’s hands? Because i have the light tweened in the local script.
Well I tried it, but there is a RunService.RenderStepped function and it cannot be played on server. What should i do then? Should i keep firing the remote event to a server inside the function and constantly changing the CFrame?
Instead of changing the brightness of the flashlight in your local script, add a remote event that fires when the player presses the key, and put the code of the brightness in a server script.
When the server script receives a call from the remote event, you have to turn on the brightness of the flashlight for the player who sent the call.
Yes, I have tried it. It works, but only for the Brightness. I would need to make the position of the light (as it is following the mouse on the screen) change on a server and I have no clue how to make it work.
yo whats up! i haven’t really read any of the posts but probably what you would need to do is create the light and flashlight on the server then give it to the player, then when you want to turn on the flashlight / change the brightness you send a remote event.
Well it’s basically the same code as you had in the local script, except that when doing it on a serverscript, you will enable the brightness (the same way as in the localscript) for everyone. So in your localscript, you probably had a line of code moving the brightness to the player’s flashlight. And it’s actually this line :
Clone.Position = cam.CFrame.Position
What you want now, is to create clones for every player (because you don’t want only one flashlight to toggle when there are many players)
To make it easy, just create an empty array flashlights_clones = {}
When a player joins (through playeradded event), you can create a clone of the flashlight (like you do in the localscript) and you add the player’s name as a key (or whatever you want) with the clone as value (it can be an instance).
Then, when you receive a RemoteEvent from the player who wants to activate their flashlight, simply replace your activation script by using flashlights_clones[plr] instead of the Clone defined in your localscript. You can also figure out if you need to send data through the remote event, such as the CFrame from where you take the position cam.CFrame.Position…
Your flashlight effect is all being handled locally (client scripts). So only you can see it. If you want that seen by all you need to use server scripts.
Yes, but how would the code look like on server script, because i do not understand quite well what do you mean. It would help a lot to see the actual code
Nevermind, I managed to fix it by creating one more flashlight that will be on the server, and when a certain remote event was detect, the spotlight was going to be enabled on local script, enabled on server, and disabled on local script the other flashlight that was enabled on server so player doesnt see two spotlights, one from local script, and one from server script. Thanks to everybody that tried to help. I appreciate it!