This is like my first time ever using userinputservice, and my script seems to not give me an error but it doesnt produce the desired output.
Basically; Player presses ‘L’, and the lamp will turn off (character spawns with it on already), and pressed again makes it turn on. But it doesnt want to turn off so I am confused.
local humanoid = script.Parent:WaitForChild("Humanoid")
local player = game.Players.LocalPlayer
local character = player.Character
--parts
local lightSource = game.StarterPlayer.StarterCharacter.LightSource
local light = lightSource.HeadSpotLight.Enabled
local lightBeam = lightSource.light.Enabled
local headlampOn = true
uis.InputBegan:Connect(function(inp, processed)
if processed then return end
if inp.KeyCode == Enum.KeyCode.L then
lightSource.Material = ("Glass")
lightSource.BrickColor = BrickColor.new("Really black")
light = false
lightBeam = false
headlampOn = not headlampOn
if inp.KeyCode == Enum.KeyCode.L and headlampOn == false then
lightSource.Material = ("Neon")
lightSource.BrickColor = BrickColor.new("Institutional white")
light = true
lightBeam = true
headlampOn = headlampOn
end
end
end)
No because the character is cloned upon spawn, there are 2 character models one in starter character and one in workspace because of the cloning as @TimeAndLocation mentioned.
Try his suggestion first. You can also print out the instance and click in console to make sure it is getting the correct instance.
local character = player.Character or player.CharacterAdded:Wait()
local lightSource = character .LightSource
print(character)
I made a few changes, and it seems to not return an error at the moment, but its not producing the desired outcome, anything I should change or implement, like remote events/server scripts so its server sided as well?
local uis = game:GetService("UserInputService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
--parts
local lightSource = character.LightSource
local headlampOn = true
uis.InputBegan:Connect(function(inp, processed)
if processed then return end
if inp.KeyCode == Enum.KeyCode.L and headlampOn == true then
lightSource.Material = ("Glass")
lightSource.BrickColor = BrickColor.new("Really black")
headlampOn = false
if inp.KeyCode == Enum.KeyCode.L and headlampOn == false then
lightSource.Material = ("Neon")
lightSource.BrickColor = BrickColor.new("Institutional white")
headlampOn = true
end
end
end)
You should revisit the logic of the code with the nested if statements, I suspect this is causing the issue. I will not give out the script and just point out the issue.
You should also view other code on how an input toggle is achieved like this one using if else