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

Bad argument at line 4 of server script? New to RemoteEvents & Local Scripts.

Asked by 6 years ago
Edited 6 years ago

I have 2 scripts which used to be 1 local script but now it is 2 scripts. What I want it to do is when a guy presses a key while he sits on the driver's seat of a bus, it will tell other scripts to do their function, for example opening the door.

I keep getting an error saying "bad argument #1 to 'lower' (string expected, got Object) at Line 4 of the Server Script.

Here are the scripts:

Local script:

1local mouse = game.Players.LocalPlayer:GetMouse()
2local event = game.ReplicatedStorage.BusEvent
3local Bus = game.Workspace:findFirstChild(game.Players.LocalPlayer.PlayerGui.TGUI.Value.Value)
4 
5event:FireServer()

Server script:

01local event = game.ReplicatedStorage.BusEvent
02 
03event.OnServerEvent:Connect(function(key)
04key = string.lower(key)
05if key == "q" then -- LEFT SIGNAL
06Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value
07Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false
08Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false
09elseif key == "e" then -- RIGHT SIGNAL
10Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value
11Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false
12Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false
13elseif key == "x" then -- HAZARDS
14Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value
15Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false
View all 31 lines...

If you know what the problem is can you please help me solve the issue! I know nothing about scripting, so please help me out!

0
why are you checking if the player equals a string? User#19524 175 — 6y
0
The original script had that line theawesomeSC4sim 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The first argument that OnServerEvent takes is always the player who fired the event. If you want to get the user's keyboard input, I suggest you use UserInputService.

Here's my take on this:

LocalScript:

1local uiService = game:GetService("UserInputService")
2local event = game.ReplicatedStorage.BusEvent
3local mouse = game.Players.LocalPlayer:GetMouse()
4local Bus = game.Workspace:FindFirstChild(game.Players.LocalPlayer.PlayerGui.TGUI.Value)
5 
6uiService.InputBegan:Connect(function(key)
7    event:FireServer(key.KeyCode)
8end)

ServerScript:

01local event = game.ReplicatedStorage.BusEvent
02 
03event.OnServerEvent:Connect(function(player, key)
04    local Bus = workspace:FindFirstChild(player.PlayerGui.TGUI.Value)
05    if key == Enum.KeyCode.Q then -- LEFT SIGNAL
06    Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value
07    Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false
08 
09    Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false
10 
11    elseif key == Enum.KeyCode.E then -- RIGHT SIGNAL
12 
13    Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = not       Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value
14 
15    Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false
View all 46 lines...

Not my most efficient code. Untested.

The UserInputService gets the player's input and then returns an InputObject. We use the KeyCode property to figure out which key they're using and then fire the RemoteEvent with the key's KeyCode as its first argument. Then, the OnServerEvent connects with the player who fired the event as its first argument and the KeyCode as its second argument.

We then see if a KeyCode matches the KeyCode we passed to the event. If it does, then you do whatever.

0
Right. I forgot to define "Bus" lol. ProtectoidZ 42 — 6y
0
The error said "attempt to index global 'Bus' (a nil value)" theawesomeSC4sim 0 — 6y
0
I fixed it. Try it again ProtectoidZ 42 — 6y
0
It still doesn't work. same problem theawesomeSC4sim 0 — 6y
0
Instead of global it is local. theawesomeSC4sim 0 — 6y
Ad

Answer this question