Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

can a Script picking up different info from RemoteEvent?

Asked by 3 years ago

I was wondering if i can send different information through a RemoteEvent, Its hard to explain so ill just show through script.

Client Script

--Client

--J
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputServer = game:GetService("UserInputService")

local plr = Players.LocalPlayer
local event = ReplicatedStorage:WaitForChild("event")

UserInputServer.InputBegan:Connect(function(input, IsTyping)
    if IsTyping then
        return
    elseif input.KeyCode == Enum.KeyCode.J then
        local J = "J"
        event:FireServer(J)
        print("Fired Server")
    end
end)

Client Script

--Client

--K
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputServer = game:GetService("UserInputService")

local plr = Players.LocalPlayer
local event = ReplicatedStorage:WaitForChild("event")

UserInputServer.InputBegan:Connect(function(input, IsTyping)
    if IsTyping then
        return
    elseif input.KeyCode == Enum.KeyCode.K then
        local K = "K"
        event:FireServer(K)
        print("Fired Server")
    end
end)

Server Script

--Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local event = ReplicatedStorage:WaitForChild("event")

event.OnServerEvent:Connect(function(J, K)
    if J then
        print("J was clicked")
    elseif K then
        print("K was clicked")
    end
end)

When I play the game and Click K It just ends up saying " J was clicked" Although I clicked K. How would I do this? Is it even possible?

2 answers

Log in to vote
0
Answered by 3 years ago

First of all, you only put in one parameter, which is a string called "K" or "J". You don't have to create another parameter for it. Also what Arson said, FireServer command on default has a player parameter.

Your code would be this:

--Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local event = ReplicatedStorage:WaitForChild("event")

--parameters player and key
event.OnServerEvent:Connect(function(player, key)
    if key == "J" then
        print("J was clicked")
    elseif key == "K" then
        print("K was clicked")
    end
end)

In short, you just needed to add the parameter of the player and only need one string parameter.

0
I would highly recommend renaming your event to something specific. CrypxticDoge 135 — 3y
0
Would i need to Specify what key is in the LocalScript? KoalaTech 6 — 3y
0
No CrypxticDoge 135 — 3y
0
You already did since you sent the string over to the server script CrypxticDoge 135 — 3y
View all comments (4 more)
0
Where did you find/get "key " from? KoalaTech 6 — 3y
0
its the parameter J or K you sent CrypxticDoge 135 — 3y
0
the parameter name doesnt have to be exactly what you name it when you fire it CrypxticDoge 135 — 3y
0
ohh Alrigh,t thankyou KoalaTech 6 — 3y
Ad
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

It is because when a remote event is sent from a client to the server, one of the parameters is the player who sent the remote event. The first parameter is always the player who sent the remote event if you are sending it from client to server. The fix would be this:

--Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local event = ReplicatedStorage:WaitForChild("event")

--Rather than check which key you pressed, you just checked if the parameter exist.
--This is the revised scrip.
event.OnServerEvent:Connect(function(player, keyPressed)
    if keyPressed == "J" then
        print("J was pressed!")
    elseif keyPressed == "K" then
        print("K was pressed")
    end
end)

0
But when i click K it just says "J was clicked", I want it to understand that J is Enum.Keycode.J and K is Enum.KeyCode.K KoalaTech 6 — 3y
0
Oh, that is because you sent in a string as the parameter, when you pressed the letter K, you sent the string "K". What you are doing in your server script is that rather than check if the key you pressed is the key you pressed, you just checked if the parameter J exist. ArsonMcFlames 363 — 3y

Answer this question