Lasers won't work

My script isn’t working. It’s supposed to shoot a laser projectile when you press space. There are two problems:

  1. It only activates when clicking
  2. It doesn’t give any output or shoot the laser.

This is my code:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Char = script.Parent

local Mouse = Player:GetMouse()

local Laser = ReplicatedStorage.Assets.LaserProjectile
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}

local DB = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space and DB == false then
		DB = true
		local LaserClone = Laser:Clone()

		local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]
		
		LaserClone.Parent = workspace
		LaserClone.Root.Position = SelectedShooter.Position
		
		task.wait(0.05)
		DB = false
	end
end)

And before you ask, yes, all the parts exist.

Any help is greatly appreciated, thank you!

1 Like

Have you tried using context action service instead of UIS? Sometimes if a button is already used (like space) UIS will eat it and it will not make it to your function.

A few things could be going wrong here, depending on what exactly “LaserProjectile” is:

  • If LaserProjectile is a Model and Root is a child part, then changing Root.Position will only move that part, even if that part is the Model.PrimaryPart. Are you trying to move a whole model? PivotTo() and SetPrimaryPartCFrame() are how you can move whole models, with the former being the latest preferred method.

  • Is the spawned part Anchored? If not, it could just be falling away the moment it’s spawned, bringing me to the next point…

  • You set this Root part position after parenting to workspace. If the part is in storage with a saved position, that’s where it will initially spawn. If this position is below the kill plane, it can instantly despawn.

  • What do you mean by “doesn’t shoot the laser”? The code here is just spawning something, there’s no code to make it move. Does this LaserProjectile thing have it’s own scripts to take care of that? You’re spawning them on the client, so the part will only be on this client, and the only kind of script that will work in it is a Script (not LocalScript) set to RunContext of Client, just FYI.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.