Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I can't damage the player, why?

Asked by 5 years ago

Every time I use my script:

script.Parent.Touched:Connect(function(tch)
    if tch.Parent:FindFirstChild("Humanoid") then
        tch.Parent.Humanoid.Health = tch.Parent.Humanoid.Health-math.random(1,10)
        wait(10)
    end
end)

I get the following error

Workspace.Part.DamageScript:3: attempt to perform arithmetic on field 'Health' (a userdata value)

If someone would help me I would be extremely grateful!

0
That code should work for a normal Roblox avatar. Have you put anything inside the character model that might create a naming conflict? Try printing out what typeof(tch.Parent) and typeof(tch.Parent.Humanoid) are, or their .ClassName properties. If they aren't both Instance types with Model and Humanoid class, something's amiss. EmilyBendsSpace 1025 — 5y

3 answers

Log in to vote
0
Answered by
Echtic 128
5 years ago

Try defining the humanoid first, and then using the :TakeDamage() event

script.Parent.Touched:Connect(function(tch)
local h = tch.Parent:FindFirstChild("Humanoid")

if h ~= nil then

h:TakeDamage(math.random(1,10))
wait(10)
end
end)

Hope this solved your problem, if not let me know we might be able to figure something else out.

0
I tried this and it didn't work. I used a script from another game I had but it also didn't work, it seems that it won't work with an R6 character for some reason LittleDoge246 0 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I have made a script like that before but it slowkills or it can insta kill

function OnTouch(hit)
hit.Parent.Humanoid:TakeDamage(5)
    end
script.Parent.Touched:Connect(OnTouch)(hit)

should work you may get an error but thats only because the part might be touching the baseplate

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

This should work for all players:

local part = script.Parent
local debounce = true

part.Touched:Connect(function(tch)
    if tch and tch.Parent and game:GetService("Players"):GetPlayerFromCharacter(tch.Parent) and debounce == true then then
        debounce = false
        local human = tch.Parent:FindFirstChildOfClass("Humanoid")
        if human then
            human.Health = human.Health - math.random(1, 10)
        else
            print("Humanoid Does Not Exist")
        end
        wait(10)
        debounce = true
    end
end)

I'm using the GetPlayerFromCharacter(character) function of the Players Service to make sure that the object touched is from a player in the game

I'm using the FindFirstChildOfClass(ClassName) function to make sure you are accurately finding the Humanoid, as you may have changed the name of it.

The debounce is since you have a wait(10) I assume you only want this to run every 10 seconds.

Answer this question