Trying to learn how to script with instances, ran into an error

I’ve tried to learn scripting in the past but I’ve mostly just used free models for my games and I have recently been learning some knowledge on scripting myself.
I’m trying to learn instances at the moment and my script won’t work, I want a killbrick to spawn in when the game is running, can anyone explain why this isn’t working?

local newPart = Instance.new("Part", game.Workspace)

newPart.Color = Color3.new(0.985946, 0, 0.0585794)

function hit()
	newPart.Touched:Connect(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
	end
end

thank you for your time

i think it has to do with your part not being positioned anywhere, try setting it to a position using Vector3.new() or set the part’s CFrame using CFrame.new()

it just spawns at the middle of baseplate

it doesn’t kill the player or put anything in the output

you’re not actually calling the hit() function

1 Like
local newPart = Instance.new("Part", game.Workspace)

newPart.Color = Color3.new(0.985946, 0, 0.0585794)
newPart.Position = Vector3.new(0,10,0)

function hit()
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
	end
end
newPart.Touched:Connect(hit)

Try this code, the hit even was in the function. I also set its position.

1 Like

this didnt work,

Workspace.Script:7: attempt to index function with 'newPart'

(it was hit.Parent but that had the same error too)

try this, you put the function name as your part that touched variable, and you also had nothing passing through the function to actually find the parent of

local newPart = Instance.new("Part", game.Workspace)

newPart.Color = Color3.new(0.985946, 0, 0.0585794)
newPart.Position = Vector3.new(0,10,0)

function partTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
	end
end
newPart.Touched:Connect(partTouched)
1 Like