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

Why is my Touched event not working when the player touches the part?

Asked by 3 years ago

So im trying to get the player, if he clicks a button(Text button) a part spawns, i got no problem with that, but when i try to make it so when the play touches the part they take damage, it doesnt work. Here is the following code

local btn = game.StarterGui.test2.test
function SpawnShadowPart()
    local Shadow =    Instance.new("Part")
    Shadow.Parent = workspace
    Shadow.Shape = "Cylinder"
    Shadow.Name = "ShadowCurse"
    Shadow.Position = Vector3.new(0,10,0)
    Shadow.Orientation = Vector3.new(0,10,0)
    Shadow.Color = Color3.fromRGB(170,0,170)
end



local shadow = game.workspace:FindFirstChild("ShadowCurse")
script.Parent.MouseButton1Click:Connect(function()
    local Shadow = SpawnShadowPart()
    Shadow.Touched:Connect(function(ShadowCurseEffect)

        local humanoid = game.Players.LocalPlayer:FindFirstChild("Humanoid")
        local maxHP = humanoid.MaxHealth


        humanoid.Health = maxHP - 0.70
        script:Destroy()
    end)
end)

1 answer

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

Your function isn't returning anything. If you wanna get the touching part, you'll need to return something. Which is an instance.

local btn = game.Players.LocalPlayer:WaitForChild("PlayerGui").test2.test --Manipulate the PlayerGui, not StarterGui. Also you can use script.Parent if your local script is a descendant of ScreenGui.
function SpawnShadowPart()
    local Shadow =    Instance.new("Part")
    Shadow.Parent = workspace
    Shadow.Shape = "Cylinder"
    Shadow.Name = "ShadowCurse"
    Shadow.Position = Vector3.new(0,10,0)
    Shadow.Orientation = Vector3.new(0,10,0)
    Shadow.Color = Color3.fromRGB(170,0,170)
    return Shadow --We need the object.
end

The reason why is because the function isn't returning anything, it's only returning nil, which WILL break your script. You need to return the object in order for it to work.

Ad

Answer this question