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

Server Event not firing?

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")

1 comment

0

if you want someone to help specify what line the error is on

1 answer

✓ Accepted answer

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)

2 comments

1

That makes sense, thanks!

1

please accept his answer if it worked.

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