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

Server Event not firing?

Asked by 4 years ago

Although I have not fully learned LUA coding, I am kinda grasping the concept. I have been working hours on a single line of code that, no matter what, brings up as " 18:47:07.519 - attempt to call a nil value" I cannot STAND this single error code. To attempt to solve the problem, I have set up two print lines; one in the local script and one in the normal script. The local print works but the normal script doesn't print.


--local local UIS = game:GetService('UserInputService') local plr = game.Players.LocalPlayer local Char = plr.Character or plr.CharacterAdded:Wait() local key = 'L' local Anim = Instance.new("Animation") Anim.AnimationId = 'rbxassetid://4906894017' local Debounce = true UIS.InputBegan:Connect (function(Input, IsTyping) if IsTyping then return end local KeyPressed = Input.KeyCode if KeyPressed == Enum.KeyCode.L and Debounce then Debounce = false local LoadAnimation = Char.Humanoid:LoadAnimation(Anim) LoadAnimation:play() print("The Crimson God Has Been Summoned; Run...") game.ReplicatedStorage.Summon:FireServer(print"Event Has Fired") end end) --Script (In Workspace if that helps) game.ReplicatedStorage.Summon.OnServerEvent:Connect(print"Message has been received")
0
if you want someone to help specify what line the error is on NubScripters 126 — 4y

1 answer

Log in to vote
1
Answered by
P100D 590 Moderation Voter
4 years ago
Edited 4 years ago

Your code doesn't work because :FireServer() and :Connect() are not being used correctly.

:FireServer() takes a value or expression to pass to the event. In your case, print"Event Has Fired" runs and prints to the console, but evaluates to nil and is not passed to the event.

:Connect() takes in a function to be called when the event is fired. For an event where the server is fired, the first argument to the function will be the player, followed by any arguments passed to :FireServer()

This can be done in two ways, the first being with a named function:

function doStuff(player, myText)
    print(myText)
    -- Other code goes here
end
RemoteEvent.OnServerEvent:Connect(doStuff)

And the second is an anonymous function:

RemoteEvent.OnServerEvent:Connect(function(player, myText)
    print(myText)
    -- Other code goes here
end)

Both are functionally identical, but the named function is more readable and allows you to call the function from somwhere other than the event.

You would fire this event from a LocalScript with the following:

RemoteEvent:FireServer(myText)
1
That makes sense, thanks! Crimsonknightzone 38 — 4y
1
please accept his answer if it worked. mixgingengerina10 223 — 4y
Ad

Answer this question