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:

local mouse = game.Players.LocalPlayer:GetMouse()
local event = game.ReplicatedStorage.BusEvent
local Bus = game.Workspace:findFirstChild(game.Players.LocalPlayer.PlayerGui.TGUI.Value.Value)

event:FireServer()

Server script:

local event = game.ReplicatedStorage.BusEvent

event.OnServerEvent:Connect(function(key)
key = string.lower(key)
if key == "q" then -- LEFT SIGNAL
Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value
Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false
Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false
elseif key == "e" then -- RIGHT SIGNAL
Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value
Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false
Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false
elseif key == "x" then -- HAZARDS
Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value
Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false
Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false
elseif key == "k" then -- KNEEL
Bus.Model.Bodykit.Blinkers.DriversCab.KneelEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.KneelEnable.Value
elseif key == "m" then -- FRONT DOOR OPEN/CLOSE
Bus.Model.Bodykit.Blinkers.DriversCab.FrontEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.FrontEnable.Value
elseif key == "n" then -- BACK DOOR UNLOCK
Bus.Model.Bodykit.Blinkers.DriversCab.RearEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.RearEnable.Value
elseif key == "l" then -- HEADLIGHTS
Bus.Model.Bodykit.Blinkers.DriversCab.HeadlightsEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HeadlightsEnable.Value
elseif key == "j" then -- INTERIOR LIGHTS
Bus.Model.Bodykit.Blinkers.DriversCab.InteriorEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.InteriorEnable.Value
elseif key == "r" then -- REVERSE
Bus.Model.Bodykit.Blinkers.DriversCab.ReverseEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.ReverseEnable.Value

end
end)

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:

local uiService = game:GetService("UserInputService")
local event = game.ReplicatedStorage.BusEvent
local mouse = game.Players.LocalPlayer:GetMouse()
local Bus = game.Workspace:FindFirstChild(game.Players.LocalPlayer.PlayerGui.TGUI.Value)

uiService.InputBegan:Connect(function(key)
    event:FireServer(key.KeyCode)
end)

ServerScript:

local event = game.ReplicatedStorage.BusEvent

event.OnServerEvent:Connect(function(player, key)
    local Bus = workspace:FindFirstChild(player.PlayerGui.TGUI.Value)
    if key == Enum.KeyCode.Q then -- LEFT SIGNAL
    Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value
    Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false

    Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false

    elseif key == Enum.KeyCode.E then -- RIGHT SIGNAL

    Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = not       Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value

    Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false
Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = false
    elseif key == Enum.KeyCode.X then -- HAZARDS

    Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HazardEnable.Value
    Bus.Model.Bodykit.Blinkers.DriversCab.LeftEnable.Value = false

    Bus.Model.Bodykit.Blinkers.DriversCab.RightEnable.Value = false
    elseif key == Enum.KeyCode.K then -- KNEEL

    Bus.Model.Bodykit.Blinkers.DriversCab.KneelEnable.Value = not   Bus.Model.Bodykit.Blinkers.DriversCab.KneelEnable.Value
    elseif key == Enum.KeyCode.M then -- FRONT DOOR OPEN/CLOSE

    Bus.Model.Bodykit.Blinkers.DriversCab.FrontEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.FrontEnable.Value

    elseif key == Enum.KeyCode.N then -- BACK DOOR UNLOCK

    Bus.Model.Bodykit.Blinkers.DriversCab.RearEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.RearEnable.Value

    elseif key == Enum.KeyCode.L then -- HEADLIGHTS
    Bus.Model.Bodykit.Blinkers.DriversCab.HeadlightsEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.HeadlightsEnable.Value

    elseif key == Enum.KeyCode.J then -- INTERIOR LIGHTS

    Bus.Model.Bodykit.Blinkers.DriversCab.InteriorEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.InteriorEnable.Value

    elseif key == Enum.KeyCode.R then -- REVERSE

    Bus.Model.Bodykit.Blinkers.DriversCab.ReverseEnable.Value = not Bus.Model.Bodykit.Blinkers.DriversCab.ReverseEnable.Value

    end
end)

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