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

Why The Output Prints "Hello" even tho It's not defined on the Remote Event?

Basically, the LocalScript's variable is chosenkey when i define it on ServerScriptService as chosen it still prints Hello why? Am I doing anything wrong, please someone take your time and explain if so

ServerScriptService

        remoteEvent.OnServerEvent:Connect(function(player, chosen)
print(chosen)
if chosen == "A" then
    print("hello")
end

LocalScript

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local list = {"A"}
local chosenKey = list[math.random(1, #list)]
remoteEvent:FireServer(chosenKey)

Output: Hello

1 comment

0

EEnergy8 it doesnt matter what you call the variable you fire, if you have it as a parameter thats not player, the server will assume that the variables correlate with each other

1 answer

Basically, when you're trying to transfer data from one script to another, whatever is in the :FireServer() -- Example: RemoteEvent:FireServer(data). When we go to the OnServerEvent and put (Player, Key) in the parameters, "Key" is already assigned to 'data' from the local script, so the name of the second parameter in the OnServerEvent doesn't matter. The only thing that matters is that you put the Player parameter first so it doesn't assign your data as the player then the rest of the parameters are stuff you're trying to send over.

Another Example:

--Local Script

local List = {"A", "B"}
local Chosen = List[math.random(1, #List)]

RemoteEvent:FireServer(Chosen)

-- Server Script

RemoteEvent.OnServerEvent:Connect(function(Player, Key) -- "Key is already automatically assigned as 'Chosen', because that's what we put in the parameters on the :FireServer()"
if Key == "A" then
	print("A was chosen!")
elseif Key == "B" then
	print("B was chosen!")
end

Hopefully this helps, post a comment if you have any other questions, I'll be happy to help.

2 comments

0

But, how can you define it "if key == "A*" how does the script know what we want to talk about/define, let's say I want to put a table there, how would i print the second item on the table?

0

yourTableHere[2]

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