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

What is wrong with this code? It should make a Turret do it's job.

Asked by 7 years ago

I was trying to make a script, what makes a part called "beam" to appear, if the turrets "range2" part is touched, having one side at the turret, and second side at another part called "regular". I think everything is good to go, but the function wont run when the turrets "Range2" part is touched.

script.Parent.Range2.Touched:connect(function(enemy)
    print("touched")
    if enemy.Parent.Name == "Enemies" then
        print("rekt")
        local mag = (script.Parent.BeamS.Position - enemy.Position).magnitude
        local beam = Instance.new("Part")
        beam.BrickColor = BrickColor.new("Lily white")
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.CanCollide = true
        beam.Size = Vector3.new(0.3,0.3,mag)
        beam.CFrame = CFrame.new(script.Parent.BeamS.Position, enemy.Position)*CFrame.new(0,0,-mag/2)
        beam.Parent = game.Workspace.CurrentCamera
    end
end)

(This is the whole script)

And the OutPut doesn't print neither "rekt" or "touched".

1 answer

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

Credit to OldPalHappy for pointing it out that local scripts can't run on the workspace.

Either Range2 hasn't loaded yet, or it doesn't exist. I'd recommend doing:

script.Parent:WaitForChild("Range2").Touched:connect(function(enemy)

end)

Also, is the script disabled?

And you are trying to access the current camera. If this is a normal script, this will not work. And using a local script to do everything here wouldn't let this code run. I'd recommend using remote events to communicate between a script, which makes the parts, and the local script, which fiddles around the player's camera.

0
I tried with both localscript and script, and didn't work, the script is not disabled, and the "Range2" does exist. and i cant use remote events sadly. User#9483 0 — 7y
0
Well you can't grab the current camera with a server sided script, you can only do it with a local script. So you need to find a way to make the script and local script communicate to each other, i.e. Remote events. UltimateRaheem 75 — 7y
Ad

Answer this question