Attempt to perform arithmetic (sub) on number and nil

I have recently started learning LUA and now I’m trying out Remote Events. This is a script that should decrease my health by 1 after clicking the Block, however for some reason the damage number turns into nil afterwards. I have no idea why this is happening. I’m very new into LUA and I have no idea how to fix this.

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HS_damage")


script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local valint = player:WaitForChild("PlayerGui").ScreenGui.Health.HEALTH
	local damage = 1
	remoteEvent:FireClient(player, damage)
end)

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HS_damage")

local function damager(player, damage)
	local valint = game.Players.LocalPlayer.PlayerGui.ScreenGui.Health.HEALTH
	valint.Value = valint.Value - damage
end

remoteEvent.OnClientEvent:Connect(damager)

Thanks for any help :grinning:

the player does not receive the player argument you are defining within your parameters of the damager function.
Use damager(damage) instead.

1 Like

Unfortunately it did absolutely nothing. The value is again nil.

Can you show the error and on what line it occurs


The 6th line is

valint.Value = valint.Value - damage

How are you replicating the UI (Did you clone int via alocalscript or a serverscript?)

I assume you mean the int changing? for that it’s local script

I have accidentally messed up with scripts, thanks this actually worked!

Whenever FireClient() is called through a RemoteEvent instance from the server side the player object pertaining to the client which is being fired by the server is required as an argument to the “FireClient()” call, this instructs the server on which client to fire, after this additional arguments are optional, on the listening side any corresponding OnClientEvent through the same RemoteEvent instance on the client side is fired whenever the aforementioned FireClient() is called. As OnClientEvent is handled locally the player object can be fetched via “game.Players.LocalPlayer” which means that any callback function connected to the OnClientEvent event listener does not require a parameter to represent this player object as it does not receive it, it only requires parameters to handle the receiving of any additional arguments passed to FireClient() when it is called.

I know this is solved but I have a few minor optimisations.

local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("HS_damage")
local Part = script.Parent

Part.ClickDetector.MouseClick:Connect(function(player)
	local valint = player:WaitForChild("PlayerGui").ScreenGui.Health.HEALTH
	RE:FireClient(player, 1)
end)
local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("HS_damage")
local player = game.Players.LocalPlayer
local valint = player:WaitForChild("PlayerGui").ScreenGui.Health.HEALTH

local function damager(damage)
	valint.Value -= damage
end

RE.OnClientEvent:Connect(damager)