Collision Damage help

Currently at work so I can’t paste the actual codes I’m using but they are very much based around this that was created via Google.

– Assuming the part is named “DamagePart”
local DamagePart = script.Parent

DamagePart.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if humanoid then
humanoid:TakeDamage(10) – Change the damage value as needed
end
end)

I’m trying to make a Monkey Ball type game and I want there to be collision damage to the players when they hit each other but everything I try especially the portion above works only a few times then stops or doesn’t work at all and I can’t even get a print(“test”) in the output. I’m not asking for a whole script, my question is, how would I go about to make it so collisions deal damage? What am I missing?
The balls are spawned within the player so they have access to the humanoid for an ontouch function. O had it so the balls had an outter sphere named DmgBall so when they hit another named DmgBall they would give both players damage.
Another question would be if this script is player>Ball>DriverSeat>Script, how I would I call the humanoid without going script.parent.parent.parent.Humanoid?

2 Likes

.touched is very unreliable, i would use alternate methods

2 Likes

doing script.parent.parent.parent.Humanoid would probably be the only way

1 Like

I would recommend using Workspace ShapeCast or Workspace:Raycast(). In terms of performance, running a raycast does not have a strong performance impact compared to .touched().

To get the humanoid you could do [script instance]:FindFirstAncestor("Name of object containing humanoid").Humanoid

Hope this helps!

1 Like

i always use this, i don’t see any alternate method for using .Touched as a hitbox function. Perhaps can you name one please?

1 Like

shapecast or raycast as xitral suggested

1 Like

alright thanks bro, but how do i make the function that goes with it, roblox doesn’t have .Raycast

1 Like

Do you know of any examples I can look into to learn about this? I’m still very much a newcomer to scripting lol. Farthest I have is understanding functions and yeah they do impact performance a bit.
I’ve seen raycast in the laser tag studio template but can’t make sense of it

try to know how to use module scripts if you can, its helpfull and beneficial

I’ve experimented with them a tiny bit but im finding it difficult to put two and two together on the differences and benefits of the different scripting methods and etc… I’m still running on 2013 Lua :laughing:

and much more things like,

Tables which is used to store almost anything

math(dot), really helpfull for values, especially positions and CFrames (position but better)

for value1, value2 in … do ,it helps ALOT

use task.wait() instead of wait() for much better results

try learning more things such as return, cooldowns (thanks to booleans) and more importantly, TweenService

don’t worry, with proper explanations from someone, you will improve alot, and faster

Tween I got down, I use that extensively especially for a lot of doors lol and animations lol. I’ve just scratched the surface with movements based on attachments. But yeah, functions, loops, tween is all I got lol I’ve never had a “need” to go this far into scripting but with the new games I’m trying to make, I’ve definitely fallen behind with it

wait, which type of game???

The important question is, how large are the objects and how fast are they moving. Touched works well for detecting collisions of slower, larger objects. They are often misused as fighting game-style hitboxes, which is why you see people not reccomend you use them.

Atleast 12x12x12 balls. The gyro balls that are in the new studio tutorial. Speed wise, depends lol. They will be set at 200 (I think it was studs per something) but very fast lol

Lol it will be a Monkey Ball type demolition derby type thing. You can find it in my group WCRE WC Research and Exploration. The demo I made is Gyro Wars. Sorry for no link, just leaving work now!

2 Likes

I’ve tried looking into raycasts and the sphere cast, I just don’t think it would work in this game. Everything will be moving too fast at the players control and the reference pages for them suggest they quote singular direction. I could be wrong because I don’t know or understand it yet. However I am deciding the keep the touched() function but will approach it in a different way to maximize performance. I do thank all that comments with their thoughts and I will lol more further into the scripts to see what I can do.

you can make a cooldown for it, and a boolean to make sure the function runs only when it reached one / both of the conditions easily

This is what i’m doing for the damage, it’s not collision based in a sense of these 2 parts hit each other, but rather an outer part that can reach the player to cause damage. And yeah I have both a cooldown and check value to prevent it from killing instantly

teamColor = script.Parent.Parent.Parent.TeamColor
function onTouched(hit)

	if hit.Parent:FindFirstChild("Humanoid") and game.Players:playerFromCharacter(hit.Parent).TeamColor~=teamColor.Value then 
		local debounce = false
		local humanoid = hit.Parent:findFirstChild("Humanoid")
		if humanoid and debounce==false then
			debounce = true

			humanoid.Health = humanoid.Health - 3

			wait(.2)
			debounce = false
		end
	end
end 


script.Parent.Touched:connect(onTouched)