I was working on a script for an ability, and I encountered an issue. In the script below, you will see I used script.Parent.Touched:Connect(function(t), however it didn't work. I tried the same exact script on a normal brick in workspace to see if I was crazy, and it worked. Here is my script:
1 | --This is the damage script inside the damage part (script is disabled until activated in next script) if it helps, this is a server script. |
2 | script.Parent.Touched:Connect( function (t) |
3 | if t.Parent:FindFirstChild( "Humanoid" ) then |
4 | t.Parent.Humanoid:TakeDamage( 15 ) |
5 | end |
6 | end ) |
script 2:
01 | --Variables may be confusing, so I will give context. (if it helps, this is a server script in serverscriptservice) |
02 | local tchfx = game.ReplicatedStorage:WaitForChild( "ThunderCharge" ) --The damage part |
03 | local Elfx = game.ReplicatedStorage:WaitForChild( "ElectricityFx" ) --This is just an effect for it |
04 |
05 | tch.OnServerEvent:Connect( function (p) |
06 | local c = p.Character |
07 | local hrp = c:FindFirstChild( "HumanoidRootPart" ) |
08 | local tchfxc = tchfx:Clone() |
09 | local ElfxC = Elfx:Clone() |
10 | tchfxc.Parent = workspace -Putting damage part in workspace |
11 | hrp.Position = Vector 3. new(hrp.Position.X,hrp.Position.Y * 2 ,hrp.Position.Z) --Putting |
12 | tchfxc.Position = Vector 3. new(hrp.Position.X,hrp.Position.Y,hrp.Position.Z) --Putting character higher in air |
13 | damage part where character is |
14 | hrp.Anchored = true --Making it so character doesn't move |
15 | wait(. 25 ) |
16 | ElfxC.Position = Vector 3. new(hrp.Position.X,hrp.Position.Y,hrp.Position.Z) --Putting effects where character is |
17 | ElfxC.Parent = workspace --Putting effects in workspace |
18 | ElfxC.Script.Disabled = false --Enablingscript inside of effect |
19 | tchfxc.Dmg.Disabled = false --Enabling damage script, which should make everything it touches do some damage |
20 | end ) |
For even more context, Second script is a function called through client by pressing a key (that is working nicely, so that isn't part of the issue. The script DOES enable when the script runs, The parts do show up at the characters humanoidrootpart, But for some reason it does not damage.
I found a solution that works for me
01 | local player = game.Players.LocalPlayer |
02 | local debounce = false --the debounce is for a cooldown |
03 |
04 | script.Parent.Touched:Connect( function (hit) |
05 | if not debounce then |
06 | debounce = true |
07 | local character = hit.Parent |
08 | local Humanoid = character:FindFirstChildWhichIsA( "Humanoid" ) |
09 | if Humanoid then |
10 | Humanoid.Health = Humanoid.Health- 15 --change the number to how much health you want the player lose |
11 | end |
12 | wait( 1 ) --change the number to how many seconds you want before it hurts you again |
13 | debounce = false |
14 | end |
15 | end ) |
This is a server script inside of the part that takes the damage.