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
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
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
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?
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…
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)
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.