Ok, I know I've asked this multiple times, but I never got an answer. So here we go again..
I've made this script, which doesn't completely work, and I know where the error is.
So can someone help me fix this?
Script:
local gun = workspace.Turret.Gun
local originpoint = gun.CFrame.p
script.Parent.Radar.radar.Touched:connect(function(plr)
if plr.Parent:IsA("Model") and plr.Parent:FindFirstChild("Humanoid") and game.Players.LocalPlayer.Character:FindFirstChild("Head") then
script.Parent.Gun.alert:Play()
local ignore = {script.Parent,game.Players[plr.Parent.Name]}
local ray = Ray.new(originpoint,plr.Parent.Head.CFrame.p.Unit*100)
local gun,position = workspace:FindPartOnRayWithIgnoreList(ray,ignore,false,true)
local beam = Instance.new ("Part")
beam.BrickColor = BrickColor.Red()
beam.Material = "Neon"
beam.Transparency = 50
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
beam.Parent = gun
--this is where the script goes wrong, it somehow doesn't create the beam, so the script stops when the beam is supposed to be touched.
local Distance = (script.Parent.Gun.CFrame.p-position).magnitude
local distance = Distance/8
repeat
beam.Size = Vector3.new(.15,.15,distance)
beam.CFrame = CFrame.new(script.Parent.Gun.CFrame.p,position)*CFrame.new(0,0,-distance/2)
distance = distance*2
wait (0.01)
until distance == Distance*2
print ("kimpie paarntie")
beam.Touched:connect(function(hit) -- this is where the script stops because there is no beam, so the script can't continue.
if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
local human = hit.Parent:FindFirstChild("Humanoid")
human:TakeDamage(10^308)
game:GetService("Debris"):AddItem(beam,.1)
end
end)
end
end)
I don't know what's wrong with the beam, so can someone help?
(Or maybe something else is wrong, if so, tell me )
Thanks for helping!

3 comments
Please comment if it's wrong , I'll try harder.
Well, yes thanks you noticed, but it's still the same, it doesn't create the beam
script.Parent.Radar.radar must be touched and the first 'if' must be true so the beam must be created. What kind of turret is this?