Script doesn't work, no errors

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    When I hold M player gets healer and particles get emited then they get disabled and cleared after I let go
  2. What is the issue?
    Script does nothing and it doesn’t show any errors, It worked as a local script.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local REGEN_RATE = 50/100
local REGEN_STEP = 0.1
local particula = ReplicatedStorage.RCTParticle

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		print(plr.Name.. " joined")
		local hrp = char:FindFirstChild("HumanoidRootPart")
		local Humanoid = char:WaitForChild("Humanoid")
		particula:Clone()
		particula.Parent = hrp
		particula.Enabled = false
	
	
	UserInputService.InputBegan:Connect(function(Input, gameProcessed) 
		if Input.KeyCode == Enum.KeyCode.M then
			while UserInputService:IsKeyDown(Enum.KeyCode.M) do
				local dt = wait(REGEN_STEP)
				local dh = dt*REGEN_RATE*Humanoid.MaxHealth
				print("M held")
				particula:Emit(25)
				Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)	
				wait(0.1)
			end
		end
	end)


	UserInputService.InputEnded:Connect(function(Input, gameProcessed)
		if Input.KeyCode == Enum.KeyCode.M then
			wait(particula.Lifetime.Max)
			print("M let go")
			particula.Enabled = false
			particula:Clear()
		end
	end)
	end)		
end)





Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Can you show where this script is located, and what type of script it is (script, or localscript) and its run context?

You said no errors, but does it show any of the print statements?

It does show the print message
image
image

You cant do this in a sever script, it needs to be on the client in a localscript.

To make my answer a bit more clear, the parts where you have InputService, detecting the keypress, is what needs to be on the local script. (also maybe the particle effect, unless you want other players to see it too)

You will need to detect the keypress on the client, local script, then send a RemoteEvent to the server, where the server can check to see if you have permission for that ability, then do the part of the script that is increasing health or showing particle effects.

I have tried to do that but the moment I press the key, the server script keeps running forever lagging the studio

You should probably show that script setup where it keeps running and is laggy, it might be easier to determine the problem with that.

I sometimes also get that error too, It could be that the script is functioning correctly, but doing it incorrectly. I would suggest adding some print() statements to show you what is working or not.

Local
local player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local character = player.LocalPlayer.Character 
local Humanoid = character:WaitForChild('Humanoid')
local hrp = character:FindFirstChild("HumanoidRootPart")
local REGEN_RATE = 50/100
local REGEN_STEP = 0.1
local particula = ReplicatedStorage.RTCParticule
particula:Clone()
particula.Enabled = false


UserInputService.InputBegan:Connect(function(Input, gameProcessed) 
	if Input.KeyCode == Enum.KeyCode.M then
		while UserInputService:IsKeyDown(Enum.KeyCode.M) do
			ReplicatedStorage.RTC1:FireServer()
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if Input.KeyCode == Enum.KeyCode.M then
		wait(particula.Lifetime.Max)
		print("M let go")
		particula.Enabled = false
		particula:Clear()
	end
end)
Server
local RepStorage = game:GetService("ReplicatedStorage")
local REGEN_RATE = 50/100
local REGEN_STEP = 0.1
local particula = RepStorage.RTCParticule
local UserInputService = game:GetService("UserInputService")

RepStorage.RTC1.OnServerEvent:Connect(function(plr,input)
	local Humanoid = plr.Character:WaitForChild("Humanoid")
	local hrp = plr.Character.HumanoidRootPart
	local dt = wait(REGEN_STEP)
	local dh = dt*REGEN_RATE*Humanoid.MaxHealth
	particula:Clone()
	particula.Parent = hrp
	particula.Enabled = false
	print("M held")
	particula:Emit(5)
	Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)	
	wait(0.1)
end)

You need to use a remote event to do this, because you have to do the input in client side, and the humanoid regen in server side.

  • Add a RemoteEvent in ReplicatedStorage, call it “HealthRegen”

  • Local Script in StarterCharacterScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local HealthRegen = ReplicatedStorage:WaitForChild("HealthRegen")
local RCTParticle = ReplicatedStorage:WaitForChild("RCTParticle")

local Character = script.Parent
local Root = Character:WaitForChild("HumanoidRootPart")

local NewParticle = RCTParticle:Clone()
NewParticle.Parent = Root
NewParticle.Enabled = false

local function SwitchRegen(State)
	if State == true then
		HealthRegen:FireServer(true)		
		while UserInputService:IsKeyDown(Enum.KeyCode.M) do
			NewParticle:Emit(25)
			task.wait(NewParticle.Lifetime.Min)
		end
	elseif State == false then
		HealthRegen:FireServer(false)		
		task.wait(NewParticle.Lifetime.Max)
		NewParticle:Clear()
	end
end

UserInputService.InputBegan:Connect(function(Input, gameProcessed) 
	if Input.KeyCode == Enum.KeyCode.M then
		SwitchRegen(true)
	end
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if Input.KeyCode == Enum.KeyCode.M then
		SwitchRegen(false)
	end
end)
  • Script in ServerScriptService:
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local HealthRegen = ReplicatedStorage:WaitForChild("HealthRegen")

local REGEN_RATE = 50/100
local REGEN_STEP = 0.1

local PlayersRegen = {}

local function SwitchRegen(Player, State)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	
	PlayersRegen[Player.Name] = State
	
	if State == true and Humanoid then
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		
		repeat
			Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)	
			task.wait(0.1)
		until
		PlayersRegen[Player.Name] == false or Player == nil or Character == nil
	end
end

HealthRegen.OnServerEvent:Connect(function(Player, State)
	task.spawn(function()
		SwitchRegen(Player, State)
	end)
end)

Doing that much calls to the remote event will broke the regen, there is a limit on how many time you can fire a remote per minute.

Uhhh it does nothing when I tried it. also no errors