Script working... only one time?

I want to detect the script, if the material is Slate or Neon…
It detects the first time, but then later…

Here is the code:

while true do
	wait(1/60)
	if script.Parent.Parent.Parent.Parent.Parent.LightFL1.Material == Enum.Material.Neon then
		script.Parent.Visible = true
		return
	elseif script.Parent.Parent.Parent.Parent.Parent.LightFL1.Material == Enum.Material.Slate then
		script.Parent.Visible = false
		return
	end
end
9 Likes

Try using continue, or just remove the return function

3 Likes

Nope:

This is the code:

while true do
	wait(1/60)
	if script.Parent.Parent.Parent.Parent.Parent.LightFL1.Material == Enum.Material.Neon then
		script.Parent.Visible = true
		continue
	elseif script.Parent.Parent.Parent.Parent.Parent.LightFL1.Material == Enum.Material.Slate then
		script.Parent.Visible = false
		continue
	end
end

3 Likes

can i ask what’s the reason behind using return in your code

(also try removing the continue)

3 Likes

I am an amateur in coding… Thought it will fix the problem, becuase it says return.

3 Likes

I tried that before. The same result.

2 Likes

remove the 1/60 its stupid just put task.wait() instead

3 Likes

It′s the same result…

while true do
	task.wait()
	if script.Parent.Parent.Parent.Parent.Parent.LightFL1.Material == Enum.Material.Neon then
		script.Parent.Visible = true
	elseif script.Parent.Parent.Parent.Parent.Parent.LightFL1.Material == Enum.Material.Slate then
		script.Parent.Visible = false
	end
end

1 Like

are u sure the material’s changing though

2 Likes

Yes. You can see it in the video.

2 Likes

What exactly are you trying to do? If you’re looking to listen for when the Material changes (I think), I would suggest using :GetPropertyChangedSignal() instead of using a while loop. Are you initially setting the materials via another script?

3 Likes

Yes. I am.

Im going to try that.

3 Likes

This would be an issue with the script changing the materials, could you post that?

3 Likes
print(script:GetFullName())
local player=game.Players.LocalPlayer
local mouse=player:GetMouse()
local Body = script:FindFirstAncestorWhichIsA('ScreenGui'):WaitForChild('Car')['Value']:WaitForChild('Body')
mouse.KeyDown:connect(function(key)
	if key=="l" then
		if Body.LightFL1.Material == Enum.Material.Slate then
		Body.LightFL1.Material = Enum.Material.Neon
		Body.LightFR1.Material = Enum.Material.Neon
		Body.LightFL2.Material = Enum.Material.Neon
			Body.LightFR2.Material = Enum.Material.Neon
		elseif Body.LightFL1.Material == Enum.Material.Neon then
			Body.LightFL1.Material = Enum.Material.Slate
			Body.LightFR1.Material = Enum.Material.Slate
			Body.LightFL2.Material = Enum.Material.Slate
			Body.LightFR2.Material = Enum.Material.Slate
		end
		print("you")
	end
end)

It′s in LocalScript…

And I also found out, that I am stupid.
I tried to Script to read a Material Change from LocalScript…

3 Likes

Haha, glad you figured it out! I would also suggest using UserInputService instead of mouse to get when player input is. Here’s an example of that;

local Player = game.Players.LocalPlayer
local UIS = game:GetService('UserInputService')
local Body = script:FindFirstAncestorWhichIsA('ScreenGui'):WaitForChild('Car')['Value']:WaitForChild('Body')

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.L then
		if Body.LightFL1.Material == Enum.Material.Slate then
			Body.LightFL1.Material = Enum.Material.Neon
			Body.LightFR1.Material = Enum.Material.Neon
			Body.LightFL2.Material = Enum.Material.Neon
			Body.LightFR2.Material = Enum.Material.Neon
		elseif Body.LightFL1.Material == Enum.Material.Neon then
			Body.LightFL1.Material = Enum.Material.Slate
			Body.LightFR1.Material = Enum.Material.Slate
			Body.LightFL2.Material = Enum.Material.Slate
			Body.LightFR2.Material = Enum.Material.Slate
		end
	end
end)
4 Likes

Thank you for the improvement!

However, I don′t know, how to connect LocalScript to Script. Could you please, help me out?

2 Likes

Sure thing. To communicate between a LocalScript and Script / Script to LocalScript, use either a RemoteFunction or RemoteEvent.

In your case, a RemoteEvent would be best as you’re not necessarily yielding for a result. RemoteEvents are like sending a message from a Script / LocalScript to a LocalScript / Script without needing a response back. Whereas RemoteFunctions are the opposite when you need a response back from the Script / LocalScript.

4 Likes

return breaks the while loop, task.wait already waits for 1/60 unless frame rate is low, use continue instead of return

3 Likes

I mentioned what you said now earlier, apparently it’s his other stuff that didn’t work

3 Likes