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