What do you want to achieve?
Basically, I tried scripting a flashlight using a localscript, and I figured that when it’s turned on it only shows that it’s on for the client, not for other people too, so I tried connecting the localscript to a server script (normal script) using a remote event and I can’t figure out why it’s not working/ why it’s not showing up on other people’s screen too!
Here’s a picture of the code in the local script along with where everything is:
In the first script you are not firing any event when the player is turning off the flashlight + you are turning on and off the flashlight on the client which you don’t have to because you are going to that in the server, and in the server, you are creating an input Began event which is not needed. In the server you just need to turn on and off the flashlight! Hope you will figure it out.
I think I got the server script part right! However, I do not understand what I need to use in the local script?
I only kept the InputBegan in the LocalScript and moved everything else into the server script, and in the server script I added FlashLightEvent.OnServerEvent:Connect(function(Player, KeyCodeTest, input)
No, the server script should not have an InputBegan connection, it’s likely everything in that connected function will never run, but even if it did you’re adding a connection every time the event fires causing a memory leak.
FlashLightEvent.OnServerEvent:Connect(function(player, keycodetest)
FlashOn = not FlashOn
keycodetest.PointLight.Brightness = if FlashOn then 1222 else 0
end)
I see! Thank you!
Sorry for the misunderstanding, I’m trying to incorporate the events in my scripts right after I watched a DevKing tutorial on them!
May I also get a hand on the localscript?
There aren’t any error logs in the log, I must mention that the tutorials I’m using to learn LUA are from 2018, so some stuff may be outdated!
I’m using the studio’s local server to see if the flashlights turns on for both players/ multiple players. Basically when one player turns on the flashlight I want all the other players to see that (to see the brightness turned up to 1222).
The problem is that it’s not turning on for everyone in the studio’s local server, it looks like the flashlight turns on separately for each client (sorry if the bold lettered part doesn’t make sense) the server script only seems to be printing “user”
Ah I see it now. you are sending KeycodeTest as part of the remote, which you should be using, but you also defined KeyCodeTest (capital C) which is in the starterpack. Nothing in the starter pack will show, I recommend deleting those variables and using KeycodeTest. Also please paste scripts with three back ticks like so
```
paste my code here!
local RS = game:GetService(“ReplicatedStorage”)
```
so it will show like so, easier to edit and read for us!
paste my code here!
local RS = game:GetService("ReplicatedStorage")
in this script you have two varibles called “keycodetest” but they alter in capitalization, only one of them should be used, the other is part of the StarterPack which isn’t relevant.
local SP = game:GetService("StarterPack")
local RS = game:GetService("ReplicatedStorage")
local FlashLightEvent = RS:WaitForChild("FlashlightEvent")
local UserInputService = game:GetService("UserInputService")
-- evil fake KeycodeTest
local KeyCodeTest = SP:FindFirstChild("Tool"):FindFirstChild("Handle")
local FlashOn = false
FlashLightEvent.OnServerEvent:Connect(function(player, KeycodeTest) -- the real KeycodeTest to use
if FlashOn == false then
FlashOn = true
print("user")
KeyCodeTest.PointLight.Brightness = 1222
elseif FlashOn == true then
FlashOn = false
KeyCodeTest.PointLight.Brightness = 0
end
end)
I would rewrite the script like so
local RS = game:GetService("ReplicatedStorage")
local FlashLightEvent = RS:WaitForChild("FlashlightEvent")
local FlashOn = false
FlashLightEvent.OnServerEvent:Connect(function(player, KeycodeTest)
FlashOn = not FlashOn
KeycodeTest.PointLight.Brightness = if FlashOn then 1222 else 0
end)
That makes so much more sense now, I thought the variable “KeyCodeTest” doesn’t transfer between scripts! Thank you so much!
One more thing, if you can name some off the top of your head, could you help me with some ideas of what other scripts to make with remote events/ functions? I’d like to exercise more with these
When you put a variable in :Fire(myVariable) it will be transferred to the connected function. It’s important to keep variables in the same order as it doesn’t match by name. Remote events always have the player as their first argument, then the transferred variables.
A flashlight is a pretty good use-case. I can’t think of other good practice that wouldn’t be nearly the same. Maybe setting an image on a billboard.