Module Damage Script Not working on server side

Recently I have been trying to utilize module scripts. I have tried to make one so were if you press your “R” key it damages your character. The damage works on the client side, but it doesn’t register on the server side. How would I fix this? Here is the code:
Local Script:

local Module = require(script:WaitForChild("ModuleScript"))
local UserInput = game:GetService("UserInputService")

UserInput.InputBegan:Connect(function(input, istyping)
	if istyping then return end
	if input.KeyCode == Enum.KeyCode.R then
		Module.Damage()
	end
end)

Module Script:

local module = {}

function module.Damage()
	print("Module Called")
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local Humanoid = char:WaitForChild("Humanoid")
	Humanoid.Health = Humanoid.Health - 10
end
return module

This is most likely because you have a variable that mentions the local player consider removing that variable. And instesd making a arugment inside the function that defines the player. Then on the other script you can give the player as the first argument when you fire the function.

ModuleScripts run in the environment that you are calling require in. If you require() a module in a LocalScript, it will run on the client. Consider using RemoteEvents, but make sure to implement checks.

Thank you for the soulution, but would there be a way to run this on the server side while being fired from the client? Or no?

You can use RemoteEvents.

Server:

SomeRemoteEvent.OnServerEvent:Connect(function(player)
    local char = player.Character or player.CharacterAdded:Wait()
    local humanoid = char:WaitForChild("Humanoid")
    humanoid:TakeDamage(10)
end)

Client:

SomeRemoteEvent:FireServer()

Check out the documentation for networking:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.