Why does the output prints "nil"?

Pretty self explanatory. I have a server-sided script placed inside a brick that fires when someone touches the brick. Code:

    script.Parent.Touched:Connect(function(hit)
	local enemyChar = hit.Parent
	local enemyPlr = game:GetService("Players"):GetPlayerFromCharacter(enemyChar)

	
	
	game.ReplicatedStorage.CameraShakeEvent:FireAllClients(enemyPlr.Name)
	print("Event fired")
	
end)

So I’m sending the enemy Players name to a client sided script placed inside startercharacterscript.
code:

    local cameraShakeEvent = game.ReplicatedStorage:WaitForChild("CameraShakeEvent")
local cooldown = 3


cameraShakeEvent.OnClientEvent:Connect(function(plr,enemyPlr)
	
	print(enemyPlr)
	for _, v in pairs(game.Players:GetPlayers()) do
		if v == enemyPlr then
			print("We gott em")
			local hum = v.Character:FindFirstChild("Humanoid")
			
			for i = cooldown,0,-1 do
				local currentTime = tick()
				local shakeX = math.cos(currentTime*10)*.35
				local shakeY = math.abs(math.sin(currentTime*10)) *.35
				local shake = Vector3.new(shakeX,shakeY,0)
				hum.CameraOffset = hum.CameraOffset:lerp(shake,.25)
				if i == 0 then
					hum.CameraOffset = hum.CameraOffset * .75
				end
				wait(1)
			end
		end
	end
end)

The output prints nil and I honestly have no clue why. Please help
Thanks in advance!

It is because you are only passing the enemyPlr.Name in the FireAllClients, and you are referencing the second arguement from that event, which is basically nil. So it can be fixed if you replace your cameraShakeEvent’s OnClientEvent to this:

cameraShakeEvent.OnClientEvent:Connect(function(enemyPlr)
3 Likes

Ahh ok thanks! Didn’t know that

Oh, so I guessed it right. Does FireAllClients not accepts any arguments?

It does, it accepts a tuple, means multiple arguments basically, which you can reference from client side

Can you use an example so I can understand it better?

I’m just gonna chime in with some more information on this. If you ever wonder what a function takes for arguments or what the purpose of it is, then the wiki is really helpful. In this case you could simply lookup FireAllClients and you would see examples of it.

2 Likes

A good example would be something simple:

--Server
local event = <pathToEvent>

event:FireAllClients("Hello", "World")

--Client
local event = <pathToEvent>

event.OnClientEvent:Connect(function(argument1, argument2)
    print(argument1, argument2) -- Will Print Hello World as that is what you pass earlier
end)
1 Like

Thanks for making me understanding better.

@Jhxan I know what’s a function, parameter and argument. No need to remind me but thanks.

That’s not what I said though. You asked if it does not take any arguments, so I was just trying to be helpful by recommending the wiki.

2 Likes