Damage loop ClickDetector script no longer doing damage to the player

A while ago I asked how I would go about damaging a player via a ClickDetector. This was the (functioning) script someone so kindly helped me make:

local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")
ClickDetector.MouseClick:connect(function(Player:Player)
	local char = Player.Character
	local hum = char:FindFirstChild('Humanoid')
	if hum == nil then return end

	repeat
		hum:TakeDamage(10) --amount it damages per loop
		task.wait(5) --time it waits per loop
	until hum.Health <= 0
end)

EDIT: it didnt paste the variables for some weird reason. anyway.

However, as my scripts like to mysteriously do, this recently broke for no discernable reason. It doesn’t output any errors into Output. It just no longer damages the player.

Did something in here get deprecated? How do I get this working again?

Thanks.

1 Like

None of the functions in your script were deprecated so everything should work. Could you paste your entire script though?

1 Like

What I posted is the whole script.

Very old code.

ClickDetector.MouseClick:Connect(function(__PLAYER)
	local __CHARACTER = __PLAYER.Character
	local __HUMANOID = __CHARACTER:FindFirstChildOfClass("Humanoid")
	if __HUMANOID ~= nil then
      --could use task.spawn() but eh
		coroutine.wrap(function()
			repeat
				__HUMANOID:TakeDamage(15)
				task.wait(3)
			until __HUMANOID.Health <= 0
		end)()
	end
end)

So, you need to do a few things.

  • Add a Remote Event named DamagePlayer (Place in ReplicatedStorage)
  • Make leaderstats, You can even make the PlrList Enum, but in order to make it work you need leaderstats.

Now, put this script in the ClickDetector.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("DamagePlayer")

local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function()
	RemoteEvent:FireServer()
end)

Now, Put this script inside the RemoteEvent.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("DamagePlayer")

RemoteEvent.OnServerEvent:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local health = leaderstats:WaitForChild("Health")
	health.Value = health.Value - 10
end)

If you dont have leaderstats, Use this script and put it in ServerScriptService:

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", stats)
	Cash.Name = "Cash" -- The name that will be displayed on the leaderboard
	Cash.Value = 0 -- How much you start with
end)

If this helps, Great, else let me know.

there are soooo many flaws with that code

Why use events for mouseclick?? this is very exploitable.

Add an anticheat system. That should do the trick.

How did you reference the ClickDetector then?

Your code works for me. So your issue may lie within your Explorer.

What if you provided images of your Explorer?

This may not be the issue, however a LocalScript that is a descendant of the workspace will not run, unless it is in a Tool or Character.

Woops. For some reason it didn’t paste the variables. Lemme edit the original post.

Why are you using RemoteEvents and Leaderstats though… you don’t need either of these. MouseClick returns the Player who clicked it, so why do literally any of this.

Nah, I don’t think this is the issue, considering this script worked before.
Here it is anyway. Very simple, dunno what you expected.
Screen Shot 2023-01-22 at 2.24.38 PM

ough… i can SEE the age on this one.
Anyway, this works. What on earth am I doing wrong in my original then.

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