Hello again!
So I made a script that would do 25 damage to the player when he touches the part. But for some reason it doesn’t work even though Roblox Studio is not giving any errors at all. Does anyone mind helping me out? Here’s the code:
local debounce = Instance.new("BoolValue")
debounce.Value = false
script.Parent.Touched:Connect(function(part2)
local Player = game.Players:GetPlayerFromCharacter(part2.Parent)
if player then
for i, TouchingParts in pairs (part2:GetTouchingParts())do
if debounce.Value == false then
if script.Parent.Parent == workspace then
debounce.Value = true
Player.Character.Humanoid.Health = Player.Character.Humanoid.Health == -25
wait(1)
debounce.Value = false
end
end
end
end
end)
Note: If the -25 shows up a line under then just know that it’s actually still in the same line with a space between it.
-HystericalBinky
This will set the health to a boolean because again ==
is an comparsion operator. If you want to make it damage, just do -= 25
Player.Character.Humanoid.Health -= 25
Also in your if player then
, you didn’t capitalize the Player
, so it would always be nil.
And to help out, there’s a few recommendations I’d do, if you just want it to damage once, you don’t need to use GetTouchingParts at all, you can just do
- Get the player and check if a player was found
- Get the palyer’s character via Player.Character or
part2.Parent
,
- Get the humanoid and subtract 25 health
And for your debounce, you can just use a variable, no need for a BoolValue.
Thank’s for the help, yet it din’t work. I’ve changed the player error to Player and did -= 25 but then it just gives me the error where it says u need 2 ='s. So then I tried doin that and once I did that instead of me taking 25 dmg I die instantly. Do u know the solution to this?
Again it’s probably an issue with the debounce, may I see your current script?
Here u go:
local debounce = Instance.new("BoolValue")
debounce.Value = false
script.Parent.Touched:Connect(function(part2)
local Player = game.Players:GetPlayerFromCharacter(part2.Parent)
if Player then
for i, TouchingParts in pairs (part2:GetTouchingParts())do
if debounce.Value == false then
if script.Parent.Parent == workspace then
debounce.Value = true
Player.Character.Humanoid.Health = Player.Character.Humanoid.Health == 25
wait(1)
debounce.Value = false
end
end
end
end
end)
Wouldn’t thsi be a simpler way to achieve what you want?
local debounce = false
script.Parent.Touched:Connect(function(part2)
local char = part2.Parent
local Player = game.Players:GetPlayerFromCharacter(char)
if not Player or debounce then
return
end
debounce = true
char.Humanoid.Health -= 25
wait(1)
debounce = false
end)
Still unsure why you need GetTouchingParts
Oh, well that helps a lot. I was following a tutorial but ran into some bugs since the video is a bit old. Still learning how to script. Also I need GetTouchingParts because they need to take dmg upon touching the part.
You want the part to deal 25 damage per time stepped on? Like so it doesn’t deal damage constantly?
The thing is, that’s already done as Touched
is fired when the part is touched, if you filter for players only, then it’ll only damage the player, the way you’re doing it currently just checks for the parts that the bodypart is touching, which doesn’t seem to be required for what you need to do?
No, it just needs to be touched, doesn’t matter which part. Thanks a lot for helping me out and making it work! I’ll make a new post if I encounter more bugs in the tutorial.
1 Like
Anytime! If you have anymore issues don’t be afraid to make another post and hopefully I can help once more!