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
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.
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)
-- 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.
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:
A new idea
A script to be given to you
Your code to be fixed
Encouragement because you “Can’t script a simple damage block”
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.
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.
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
I hope everything continues to go well with your development!