Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

OnServerEvent sends player name. How do I send a string value instead?

This is a script for changing the time in my game. When a script sends a value with the :FireServer() event, its suppost to change the time depending on the value in this script

RepStore = game:WaitForChild("ReplicatedStorage") --REPLICATED STORAGE

RepStore.Events.UI.ChangeLighting.OnServerEvent:connect(function(plr, Time) --BEGIN OF SCRIPT
	print(Time)
	--SUNRISE
	if Time == string.lower("Sunrise") then
		game.Lighting.TimeOfDay = "06:10:00"
		print("[LIGHTING SYSTEM] - Successfully changed to Sunrise")
		end
	--DAY TIME
	if Time == string.lower("Day") then
		print("[LIGHTING SYSTEM] - Successfully changed to Day time")
		game.Lighting.TimeOfDay = "12:00:00"
		end
		
		--SUNSET
		if Time == string.lower("Sunset") then
		print("[LIGHTING SYSTEM] - Successfully changed to Sunset")
		game.Lighting.TimeOfDay = "18:00:00"
		end
		
		--NIGHT TIME
		if Time == string.lower("Night") then
		print("[LIGHTING SYSTEM] - Successfully changed to Night time")
		game.Lighting.TimeOfDay = "00:30:00"
		end
		
		if Time ~= 
			string.lower("Sunset") or 
			string.lower("Day") or 
			string.lower("Night") or 
			string.lower("Sunrise") then
		error("[LIGHTING SYSTEM] - Invalid Request: Does Not Exist")
		end
		
	
end) --END OF SCRIPT

The problem is when I fire the server while testing from the command bar, it sends a player name instead of the value I requested, and the failsafe at the bottom saying its invalid. I want to send it a string value, but It never works

Command line: game.ReplicatedStorage.Events.UI.ChangeLighting:FireServer(Night)

5 comments

0

Have you tried putting Night in quotes? That could be the reason. Otherwise, the script sets it so that the string value must all be lowercase. Between line 4 and 5, try putting: Time = string.lower(Time).

0

I tried that, but that only fixes part of the problem. When I "game.ReplicatedStorage.Events.UI.ChangeLighting:FireServer("Night")", It sends my username, not my value, named Time

0

By the way instead of that failsafe being how it is, you could just write a elseif's with a final else being the error. It'll shorten your code a bit.

0

Can you print(plr) and see what that gives you in output? I'm for sure that client firing sends player name as the first argument but you did it correctly so it's a bit odd.

0

When I Do: "game.ReplicatedStorage.Events.UI.ChangeLighting:FireServer(plr, Night)", When It prints print(plr) and print(Time), it comes out as >Awesomewebm >nil

1 answer

✓ Accepted answer

Hello.

This is how I would do it. (Example - tested and is very functional, in other cases, it does work)

-- Server Script: (I recommend you place the following 'ServerScript' in ServerScriptService, I also added an example of basic RemoteEvent encryption for better protection, although I don't use the method I've given you, it can still be effective against those whom just find a leaked script and execute it, expecting it do work. - Future reference, you'll understand if you work on or create a popular game sometime in the future, or if you currently are).

GlobalKey = "key123" -- I recommend you generate a 20-odd character key, with many symbols, numbers, letters, brackets, etc. (You will only need to change this).

game.ReplicatedStorage.ChangeLighting.OnServerEvent:connect(function(Player, Key, Data) -- RemoteEvent that requires / contains three (3) arguments.

if Key == GlobalKey then -- Basic encryption (If the key doesn't match, the Player will be kicked from the server to prevent RemoteEvent exploiting. You do not need to modify this line).

if Data == "Night" then
	game.Lighting.TimeOfDay = "00:00:00" -- Equal to midnight.
		end
		else
		Player:Kick("[Detected]: Event tampering...") -- Self explanatory.
	end
end)

game.ReplicatedStorage.ChangeLighting:FireServer(Key, "Night") -- No need to add the 'Player' argument as it is automatically parsed via :FireServer().

I also noticed that you did the following:

game.Lighting.TimeOfDay = game.Lighting.TimeOfDay = "00:00:00"

Unfortunately, that will not work. I have corrected that for you.

Regards, StoleYourClothes.

This discussion is preserved as it was. New answers, comments, and votes are closed.