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

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

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

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.

2 comments

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.

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.

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