Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I'm trying to make a turret that shoots bullets. Is anything wrong with this code?

Hello. So basically, I wanted to make a turret. I made a script, and there seem to be errors. What is the problem? Im... not that experienced with coding. Here is the code:

part = script.Parent


while wait(0.01) do
for i,v in pairs(workspace:GetDescendants()) do
	if v.Name == "Humanoid" then
		humanoid = v
end	
end
	if humanoid.Parent == "Finty_james" then
		return
	end
	else 
	local torso = humanoid.Parent.Torso
	local bullet = game.ReplicatedStorage.Bullet
	local newBullet = bullet:Clone()
	part.Anchored = false
	part.Orientation = torso.Orientation
	part.Anchored = true
	newBullet.Parent = workspace
	newBullet.Position = part.Position
	newBullet.AssemblyLinearVelocity = Vector3.new(torso.Position)
	part.Anchored = false
end

This is my code. Since all the parts couldn't stay in place (Due to the Gravity Property, which I won't be changing,) this is the reason why I used the anchored property 3 times. Also, the Velocity Statement is kinda like a Body Force, but it's a property and can be altered by the Gravity Property of the Workspace Object In Roblox studio.

4 comments

0

are there error messages?

0

Yes

0

Also why I got the humanoid, is because I wanted to make it damage a NPC, but it wouldnt damage me. But I wasnt focusing on that.

0

Also why I got the humanoid, is because I wanted to make it damage a NPC, but it wouldnt damage me. But I wasnt focusing on that.

1 answer

if humanoid.Parent.Name == "Finty_james" then -- you forgot the .Name
        return
    --why is there an 'end' here?
    else 
    local torso = humanoid.Parent.Torso
    local bullet = game.ReplicatedStorage.Bullet
    local newBullet = bullet:Clone()
    part.Anchored = false
    part.Orientation = torso.Orientation
    part.Anchored = true
    newBullet.Parent = workspace
    newBullet.Position = part.Position
    newBullet.AssemblyLinearVelocity = Vector3.new(torso.Position)
    part.Anchored = false
end

This discussion is preserved as it was. New answers, comments, and votes are closed.