Issues with blocks that lose health upon clicking

Hi! I’m currently messing around in a game that I’m making, I’m trying to make it so if you click one of the many parts, it loses health. Once it loses all of its health, it is destroyed, and a leaderstat point is awarded.

Currently I have it so it gets the player’s mouse, then if the player holds down, it checks if the selected part can be destroyed. If it can, it continues, if not it stops. Then it checks if the blocks health it above 0, if so it will lose one health point every 0.3 seconds.

My current problem is that I currently have the script in a local script (things will only effect the client, which is one problem with my current script), and when I try to move it into a server sided script, it just doesn’t work at all since you can’t get the player’s mouse in a server script.

Although some of the checks are working, even now while it partially works it won’t change the blocks int value health.

This is my current script. (its in a local script in starter character scripts)





mouse.Button1Down:Connect(function(Player)
	if mouse.Target.CanDestroy == true then
		local target = mouse.Target
		if target.Health.Value > 0 then
			target.Health.Value = target.Health.Value - 1
			wait(0.3)
		end
			
	end
end)```

Please let me know on what I should change, thanks!
1 Like

You must handle the deduction of the health on the server.

How would I do that though? I have been looking around on the dev hub and some other places but I can’t seem to find an answer.

You can do so with a remote event, though I suggest creating a server script that runs through all of the available blocks and adds a click detector to each and every one. Then, you could make a script that handles when the player clicks the via the ClickDetector.MouseClick() function.

Thanks, I’ll try it and see if it works.

I wouldn’t suggest using click detectors for this as it can get really messy. For example, what if you want to add a feature where the player must hold down their mouse to take health from the block.

Make a remote event like someone had said. Fire it if the block has over 0 health, from there you can subtract the health from a server script.

Yes, that is true. If he wants to do so, he should move to proximity prompts.

I currently have it so a selection box pops up when the mouse hovers over the certain parts.

Where should I keep the remote event stored?

You should put it in replicated storage.

Here is the API documentation on remote events: