Can't script a simple damage block

I can’t write a very simple damage script properly. I tried changing certain signs, etc. Can’t find a solution. The damage block is CanCollide=false. When I walk it works just fine. When standing still, It does do some damage, but not consistently.

Round script:

repeat
		r.Position += Vector3.new(2,0.1,2)
		print(r.Position)
		wait(0.5)
		r.Position += Vector3.new(-2,-0.1,-2)
		print(r.Position)
		time -= 0.5
until time <= 0

Damage script:

local enabled = true

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		if enabled == false then
			return
		end
		enabled = false
		part.Parent.Humanoid.Health = part.Parent.Humanoid.Health - 5
		wait(0.5)
		enabled = true
	end
end)

Here’s what happens:

Works (kinda)

Doesn’t work

Note that I changed nothing!

I don’t understand this. I feel like I should be better than this :cry:

1 Like

You should probably not use CanCollide here; instead you should check all players to see if they are below a certain height. CanCollide is computationally expensive.

1 Like

I don’t understand how:

  1. this could be a solution, since that shouldn’t affect a damage script, and
  2. how it would apply to a flash flood disaster survival game.
1 Like

Sorry I said CanCollide above, I should have said Touched.

You just have to do damage to all players below the height of the water.

1 Like

That’s a lot more difficult, but I’ll try it. I’ll tell you how it goes.

1 Like

You could try to see if this works. Though I agree with @TriplePattyBurger that doing damage to all player below the water level is better than using Touched in this scenario.

local touching = false

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
        touching = true
        while touching == true do
		    part.Parent.Humanoid.Health = part.Parent.Humanoid.Health - 5
		    wait(0.5)
        end
	end
end)

script.Parent.TouchEnded:Connect(function(part)
    if not part.Parent:FindFirstChild("Humanoid") then
        touching = false
    end
end)
2 Likes

This seems to kill the player almost instantly. I’ll try the water level script.

1 Like

Let me know how this goes:

-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- Variables
local waterLevel = -- However high the water is (could use water.Position.Y maybe)

local function damagePlayer(player)
    player.Humanoid.Health -= 5
    wait(0.5)
end

RunService.Heartbeat:Connect(function(step)
    for i, player in pairs(Players:GetPlayers()) do
        local playerY = player.Character.HumanoidRootPart.Position.Y
        if playerY >= waterLevel then
            damagePlayer(player)
        end
    end
end

Hope this helps.

Note: Make sure to set a value to waterLevel, that is, how high the water is in studs above the ground. If you can get the height of the water with ````water.Position.Y``` or something, that’d work.

4 Likes

I understand the processes behind a height script, it’s just more difficult and thus more time-consuming.

I will try this one but in the future I would rather have the principles be explained to me. Especially since this is for a commission.

1 Like

Ah sorry, you never specified you didn’t want code; if you did I would’ve been happy to conceptually explain it to you. Beginning with your topic’s title, “Can’t script a simple damage block,” it’s unclear what you want; you’re just stating that you can’t script something. Then you show your code and what’s going wrong, and proceed to say this:

Even up to here it’s not clear if you want:

  1. A new idea
  2. A script to be given to you
  3. Your code to be fixed
  4. Encouragement because you “Can’t script a simple damage block”
  5. Something else

Then when I tried to help by giving some code as a starting point you said the above. And up till then you never stated this was for a commision. Just, from now on it would be nice if you could be more specific to your needs for everyone that’s trying to help you. You can’t diagnose someone’s problem without knowing what needs to be fixed. I don’t mean to be rude, but I’m simply saying that it would be nice if you could be more clear with what you want as a solution.

I hope everything goes well!

3 Likes

Well I think all of that should be irrelevant, as spoonfeeding is not allowed on the forum anyways. I understand though, I should have specified that I coded the rest of the game.

Also, after some heavy tweaking, your script does work. It needed quite a bit, though (not saying it was bad, just saying it didn’t fit the game). So I guess we co-wrote it, therefore not really spoonfeeding.

2 Likes

Thanks for understanding. And not necessarily that you coded the rest of the game, but that you don’t want a full script, because even if you did code the rest of the game that doesn’t mean you don’t want to be helped through a script.

Well technically spoonfeeding is allowed on the Roblox Developer Forum. Instead, it’s asking to be spoonfed that’s against the rules.

That’s good to hear; I didn’t intend to make it fit the game, it was just a sort of “boiler plate” for what you want to accomplish. And no need to say we co-wrote it haha, it’s your script my guy :joy:

I hope everything continues to go well with your development!

2 Likes

Unfortunately for some reason the script decided to stop working :confused: but I think I know how to fix it.

1 Like