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?
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)
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)
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.
ough… i can SEE the age on this one.
Anyway, this works. What on earth am I doing wrong in my original then.