Can't figure out why the remote event doesn't work

  • 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:

  • Here’s a picture of the code in the server script:

Any help is appreciated! Thank you!

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.

1 Like

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)

This is what you want for your server script.

1 Like

LocalScript looks like this now

Server Script looks like this

Farthest I’ve gotten with this so far since I managed to print “user” in the server script as well

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?

The local script seems fine, is there any errors in the log? Can you explain what you are expecting to happen and what really happens in detail?

You might want to also check input.UserInputState == Enum.UserInputState.Begin if you want the flashlight to toggle instead of holding

Of course!

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”

if you are in the server view can you see the flashlight turning off and on?

Nope, doesn’t show up unfortunately

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")

Pardon me, I do not understand what you mean by that, also, thank you for showing me how to paste scripts like that! I forgot that was a thing!

Here are the scripts:
Local Script

local RS = game:GetService("ReplicatedStorage")
local FlashlightEvent = RS:WaitForChild("FlashlightEvent")
local UserInputService = game:GetService("UserInputService")
local KeyCodeTest = script.Parent
local FlashOn = false
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and FlashOn == false then
		FlashlightEvent:FireServer(KeyCodeTest)
        FlashOn = true
		print("user")
		KeyCodeTest.PointLight.Brightness = 1222
		
	elseif input.KeyCode == Enum.KeyCode.F and FlashOn == true then
		FlashlightEvent:FireServer(KeyCodeTest)
		FlashOn = false
		KeyCodeTest.PointLight.Brightness = 0
	end

end)

Server Script

local SP = game:GetService("StarterPack")
local RS = game:GetService("ReplicatedStorage")
local FlashLightEvent = RS:WaitForChild("FlashlightEvent")
local UserInputService = game:GetService("UserInputService")
local KeyCodeTest = SP:FindFirstChild("Tool"):FindFirstChild("Handle")
local FlashOn = false
FlashLightEvent.OnServerEvent:Connect(function(player, KeycodeTest)
	if FlashOn == false then
		FlashOn = true
		print("user")
		KeyCodeTest.PointLight.Brightness = 1222
		
		
	elseif FlashOn == true then
		FlashOn = false
		KeyCodeTest.PointLight.Brightness = 0
	end

end)

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)
1 Like

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.

1 Like

I did not know that! Again, thanks a lot! Have a good day

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.