local Damage = script.Parent.Damage.Value
function onTouched(hit)
if hit:IsA("BasePart") then
local human = hit.Parent:findFirstChild("Humanoid")
if (human ~= nil) then
human.Health = human.Health - Damage
end
end
end
script.Parent.Touched:connect(onTouched)
Ive fixed that in an earlier attempt.
Genuinely one of my typical problems, nobody can find the issue lol.
Is it possible to send me a place file so I can solve it?
I would suggest not using .Touched and using raycasts
This should provide a good way to do it!
You could just do Humanoid:TakeDamge(–insertamounthere)
After the part would touch a humanoid you would destroy it tooHumanoid:TakeDamage
I downloaded the place, and I got it to work for me, originally with remote events, but then without.
When I opened it, the main script for doing damage did not have the correct amount of .Parents
.
Here is what it looked like:
local Damage = script.Parent.Damage.Value
Now in your original script (your screenshot on your first post) it seems you didn’t have .Value
at the end of Damage
. You said it didn’t appear to do anything though, so I’ll get to why in a second.
What I did to get it to work was just change
local Damage = script.Parent.Damage.Value
into
local Damage = script.Parent.Parent.Parent.Damage.Value
However, I don’t think the main script was actually the problem. In the local script, you enable and disable the script, but you cannot do that, so the script remains disabled.
What I think you’re trying to do is when the player equips the tool and clicks, they can deal damage for one second. First, enable the main script.
Originally, I put up an explanation of how I would go about doing this with remote events, but that led to several problems, so instead I did it a simpler way. (That’s why it’s edited)
In the local script, that plays the animation. We do not need it to do anything with other things other than playing the animation. So, just delete the stuff you do not need, and it should look like this:
Now, in the main script is where it should do damage. We can add a Boolean value to see if one second has passed, and if it has, we stop doing damage, and to start doing damage, all we have to do is connect the function the same way we did with the animation, making sure the value is true. Example:
End Result:
Sorry if this was a bit lengthy, this is my first post.
game:GetService(“RunService”).HeartBeat:Wait()
script.Parent.Touched:connect(onTouched)
Delaying the script for a single frame would yield no siginificant change.
disable the damagescript instead of changing can touch property
Better yet, just use a simple debounce technique (via a state variable or the tool’s ‘Enabled’ property).
also set the handle’s cantouch property to false
This would invalidate the purpose of the tool handle’s ‘Touched’ event/signal.
turn the kill script into a localscript
This is a very bad suggestion, damage checks should always be performed on the server.
actually no, when you change health property from a humanoid it dies for the server
This isn’t true, changes to a humanoid’s ‘Health’ property done on the client will not replicate to the server.
however it would work without using complicated remotes
RemoteObjects (remote events and/or remote functions) are not required and will only further bloat the codebase and increase instance dependency, a single isolated server script will suffice (no explicit network communication is required).
I think that this should work for me, its just i suddenly get an error (dont even think its from the script changes)
Players.pap1io.Backpack.TestSword.LocalScript:6: attempt to index nil with ‘WaitForChild’
Well, I changed Char = LocalPlayer.Character
to
Char = LocalPlayer:WaitForChild("Character")
, and now it says that there is an infinite yield possible, which isn’t an error, but is still a warning.
It has worked however barely, the tool click works once and if i try again it just breaks.
That’s strange, it works fine for me.
What exactly is happening? Could you send the screenshots of your scripts?
What is the problem that happens?
robloxapp-20220706-2012395.wmv (2.9 MB)
sword doesnt have animation and just kills the npc when touched.
oh wow ofc the video didnt send
Are you looking for a slash animation to happen when you click it?
Yeah i am, should be working dk why it isnt
I fixed up my code from earlier. I didn’t know if you wanted to deal 10 damage every one second or be able to deal damage with no pause for one second (sorry for the confusing wording), so here is what I did if you wanted 10 damage every second:
In the main script, add a variable called otherval, and set it to false.
Add wait(1)
and otherval = false
into the last if statement (also you can just do if human
instead of human ~= nil
)
After the if hit:IsA("BasePart") then
add if not otherval then
and inside that indent otherval = true
Should look like this:
Basically the otherval acts as a debounce so it doesn’t automatically kill the NPC now.
Edit: Got the animation to work. Messed around with the script, and published the animation and used that ID. It didn’t work before I used that ID though, so maybe try re-publishing the animation and setting the owner to the animation as the same owner as the game if you haven’t already. Now for the script:
Delete the Humanoid
, Player
, and LocalPlayer
variables. Now we should have the Char
, Animation
, AnimationTrack
, and Tool
variables.
Change the Char
value to Tool.Parent
, and the AnimationTrack
value to Char.Humanoid:LoadAnimation(Animation)
Move the variables Char
and AnimationTrack
inside of the function before the AnimationTrack:Play()
Should look like this, except some of my variables have different names:
End result with the new code: (sorry I didn’t do the debounce thing the first time)