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 4 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

01local btn = game.StarterGui.test2.test
02function SpawnShadowPart()
03    local Shadow =    Instance.new("Part")
04    Shadow.Parent = workspace
05    Shadow.Shape = "Cylinder"
06    Shadow.Name = "ShadowCurse"
07    Shadow.Position = Vector3.new(0,10,0)
08    Shadow.Orientation = Vector3.new(0,10,0)
09    Shadow.Color = Color3.fromRGB(170,0,170)
10end
11 
12 
13 
14local shadow = game.workspace:FindFirstChild("ShadowCurse")
15script.Parent.MouseButton1Click:Connect(function()
View all 26 lines...

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 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.

01local 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.
02function SpawnShadowPart()
03    local Shadow =    Instance.new("Part")
04    Shadow.Parent = workspace
05    Shadow.Shape = "Cylinder"
06    Shadow.Name = "ShadowCurse"
07    Shadow.Position = Vector3.new(0,10,0)
08    Shadow.Orientation = Vector3.new(0,10,0)
09    Shadow.Color = Color3.fromRGB(170,0,170)
10    return Shadow --We need the object.
11end

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