Issues in the code, need help with picking, etc

Hi everyone, who is reading this topic. I glad to share with starter problem in my begging as a newbie scripter. So, on the photo you can see that there are 4 laser and walls (in model “WallParts”) so, idk what i was trying do but i add “NumberValue” for every laser and “wall” so like if the picked laser is “Laser1” (on number “1”) so it will be going to wall forward from laser like where is the Laser2 there is wall1, etc. so after thinking for a while i just give up cuz i have night rn so my braine is frizzy. Im sure that someone can advise me somethin to solve the issues in this script or even share with me full working part or full script with changed where it needs. Thank you who read that! :slight_smile:
→ PHOTO:

→ SCRIPT:

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local workspace = game:GetService("Workspace")

local function spawnLaser()
	local running = true
	
	local lasers = rs.Lasers:GetChildren() -- from Folder
	local randomLaser = lasers[math.random(1, #lasers)]
	local cloneLaser = randomLaser:Clone()
	cloneLaser.Position = randomLaser.Position
	cloneLaser.Parent = workspace
	
	cloneLaser.Touched:Connect(function(hit)
		if hit:IsDescendantOf(workspace.WallParts) then
			cloneLaser:Destroy()
		end
	end)
	
	while running do
		if cloneLaser then
			local wallsParts = workspace:WaitForChild("WallParts")
			local walls = wallsParts:GetChildren()
			
			local targetWall = nil
			for _, wall in ipairs(walls) do			
				local wallNum = wall:FindFirstChildOfClass("NumberValue")
				local laserNum = cloneLaser:FindFirstChildOfClass("NumberValue")
				print("Current of wall is: ", wallNum)
				print("Current of laser is: ", laserNum)
				
				if wallNum and laserNum and wall.Value == cloneLaser.Value then
					targetWall = wall
					break
				end
			end

			if targetWall then				
				if targetWall.Value == cloneLaser.Value then
					local startPos = cloneLaser.Position
					local endPos = targetWall.Position

					local goal = {}
					goal.Position = endPos

					local moveInfo = TweenInfo.new(
						4,
						Enum.EasingStyle.Linear,
						Enum.EasingDirection.In
					)

					local moveTween = ts:Create(cloneLaser, moveInfo, goal)
					moveTween:Play()
				end
			end
		end
		
		running = false
	end
	
	running = true -- i know that there are some sh*ts, so yep. im still newbie and im so sorry for that(
end

while true do
	spawnLaser()	
	task.wait(1)
end

4 Likes

I am sorry, I got confused on this part.

Can you reword of what your trying to say?

3 Likes

yep. sorry my english is not good in some case..
So, i wanna do like “laser game” (js to practice), there are lasers (Laser1, Laser2, Laser3, Laser4) ← They are in the Folder in “ReplicatedStorage”.
The function starts like, pick up random laser → (for instance if the laser is “Laser1” the wall will be where is “Laser2” like forward from the Laser.

So, i messed up everythig that i coukd in the script cuz im a bit sick and have night rn so i did what i could, i already know that there i shouldnt add: running in wrong place, etc
And i added “NumberValue” for each laser/wall cuz i dont know how to do it in other way to fins solution, do your best!
The issus rn what i can see in Output is: Value is not a valid member of Part “Workspace.WallParts.wd1”
“wd1” → is the wall1 (i js signed like that)

1 Like

Ah, okay I see, you might just have to put everything in one folder and create one script if you want it to work how according. There are ways to do it separate but it is too hard for me to thing.

1 Like

maybe, but as i know there are not any issues with that so i still will be waiting for solution. thanks for your mind anyways!

2 Likes
local laserTable = {
	['Laser1'] = 'Wall1',
	['Laser2'] = 'Wall2',
	['Laser3'] = 'Wall3',
	['Laser4'] = 'Wall4'
}

for laserName, wall in pairs(laserTable) do
	print(laserName, wall)
end

instead of using a NumberValue, you can use a table to check which wall is linked to each laser.

4 Likes

Oh, that’s what he meant. I thought he meant like Laser1 is connected to Wall2 or something. But yeah this might be the solution.

1 Like
ServerScript
--ServerScript in ServerScriptService
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local lasersFolder = ReplicatedStorage:WaitForChild("Lasers")
local wallsFolder = Workspace:WaitForChild("WallParts")

local walls = wallsFolder:GetChildren()
table.sort(walls, function(a, b)
	return a.Name < b.Name
end)

local lasers = lasersFolder:GetChildren()
table.sort(lasers, function(a, b)
	return a.Name < b.Name
end)

local function spawnLaser()
	local randomIndex = math.random(1, #lasers)
	local cloneLaser = lasers[randomIndex]:Clone()
	cloneLaser.Position = lasers[randomIndex].Position
	cloneLaser.Parent = Workspace

	local targetWall = walls[randomIndex + 1]
	if targetWall then
		local tween = TweenService:Create(cloneLaser, TweenInfo.new(4, Enum.EasingStyle.Linear), {Position = targetWall.Position})
		tween:Play()
	end

	cloneLaser.Touched:Connect(function(hit)
		if hit:IsDescendantOf(wallsFolder) then
			cloneLaser:Destroy()
		end
	end)
end

while true do
	spawnLaser()
	task.wait(1)
end

Should automatically match each laser to the “next wall” in the sequence without relying on NumberValues.. At least that’s what I was going for. Tried to stick with your format here.

1 Like

Thank you certainly for your solution. I definitely will use it in my script to find out if it works. If it works, I will mark it as a solution.

1 Like

i think the issue is

if wallNum and laserNum and wall.Value == cloneLaser.Value then
					targetWall = wall
					break
				end

both clonelaser and wall is an instance
unless they both have a child called value on both sides it will return nil
then you comapre the 2 nil = nil wich is true

1 Like

thanks so much for your help, it realy works better rn but after recode i have one problem for now, i don’t really understand why it doesnt work, so it should touch dissapears but it doesnt so what the problem is?

PHOTOS:

RECODED SCRIPT:

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local ws = game:GetService("Workspace")
local players = game:GetService("Players")

local running = true

local function laserSpawn()
	while running do		
		
		local laserTable = {
			["Laser1"] = "Wall1",
			["Laser2"] = "Wall2",
			["Laser3"] = "Wall3",
			["Laser4"] = "Wall4"
		}
		
		local lasersName = {}
		for laserName in pairs(laserTable) do
			table.insert(lasersName, laserName)
		end
		
		local randomLaser = lasersName[math.random(1, #lasersName)]
		local targetWall = laserTable[randomLaser]
		print(randomLaser, targetWall)
		
		local lasers = rs:FindFirstChild("Lasers")
		local walls = ws:FindFirstChild("WallParts")
		
		local laser = lasers:FindFirstChild(randomLaser):Clone()
		laser.Parent = workspace
		local wall = walls:FindFirstChild(targetWall)
		
		if laser and wall then
			local startPos = laser.Position
			local endPos = wall.Position
			local goal = {}
			goal.Position = endPos
			
			local moveInfo = TweenInfo.new(
				4, 
				Enum.EasingStyle.Linear,
				Enum.EasingDirection.In
			)
			
			local moveTween = ts:Create(laser, moveInfo, goal)
			moveTween:Play()
		end
		
		laser.Touched:Connect(function(hit)
			if hit:IsDescendantOf(workspace.WallParts) then
				laser:Destroy()
			end			
			
			local player = players:GetPlayerFromCharacter(hit.Parent)
			if player then
				player.Character.Humanoid.Health = 0
			end
		end)
		
		running = false
	end
end

laserSpawn()

1) This part doesn’t work literally

laser.Touched:Connect(function(hit)
			if hit:IsDescendantOf(workspace.WallParts) then
				laser:Destroy()
			end	

2) This works correctly

local player = players:GetPlayerFromCharacter(hit.Parent)
			if player then
				player.Character.Humanoid.Health = 0
			end

So, im waiting for your help and advise! :blush:

1 Like

Is your wall actually inside the WallParts? And you really want the laser to disappear when it touches the part, right?

1 Like

Yes, every wall is just BasePart and → WallParts is a Model, and when the laser touches the wall , it should disappears and the cycle repeats again
(this how it must works)

1 Like

Im assuming the lasers are anchored and just being tweened, which is why they wont fire any Touched events. For Touched to fire, at least one of the Partshas to be unanchored to perform physics calculations.

You could solve this by starting a second thread that waits until the tween finishes and then, if the laser still exists, destroy the laser.

Example:

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local ws = game:GetService("Workspace")
local players = game:GetService("Players")

local running = true

local function laserSpawn()
	while running do		
		
		local laserTable = {
			["Laser1"] = "Wall1",
			["Laser2"] = "Wall2",
			["Laser3"] = "Wall3",
			["Laser4"] = "Wall4"
		}
		
		local lasersName = {}
		for laserName in pairs(laserTable) do
			table.insert(lasersName, laserName)
		end
		
		local randomLaser = lasersName[math.random(1, #lasersName)]
		local targetWall = laserTable[randomLaser]
		print(randomLaser, targetWall)
		
		local lasers = rs:FindFirstChild("Lasers")
		local walls = ws:FindFirstChild("WallParts")
		
		local laser = lasers:FindFirstChild(randomLaser):Clone()
		laser.Parent = workspace
		local wall = walls:FindFirstChild(targetWall)
		
		if laser and wall then
			local startPos = laser.Position
			local endPos = wall.Position
			local goal = {}
			goal.Position = endPos
			
			local moveInfo = TweenInfo.new(
				4, 
				Enum.EasingStyle.Linear,
				Enum.EasingDirection.In
			)
			
			local moveTween = ts:Create(laser, moveInfo, goal)
			moveTween:Play()

            task.spawn(function()
                 moveTween.Completed:Wait()
                 if laser then laser:Destroy() end
            end)
		end		
			
			local player = players:GetPlayerFromCharacter(hit.Parent)
			if player then
				player.Character.Humanoid.Health = 0
			end
		end)
		
		running = false
	end
end

laserSpawn()

In short: If both parts are anchored, no physics calculations will be performed. Instead you can start a new thread to wait for the tween to finish and then destroy the laser if its still there.

3 Likes

oh, okay seems that this is good actually, i learned and saw something new for me, i will test it, thank you!! And wanna ask u if i can use it everywhere like this part of script is static doesn’t change ?

1 Like

Elaborate on what exactly you mean by using it everywhere

1 Like

hey man, just out of curiosity, try making a folder called ‘WallParts’ instead of a model, that worked perfectly for me

1 Like

Are the parts you are using for your example all anchored?

1 Like

yes, all the parts are anchored

1 Like

What did you use to trigger the .Touched

1 Like