Hello everyone, I am trying to make a script that rotates a part when I press r, but for some reason im getting an objectvalue’s value nil.
Script:
local useri = game:GetService("UserInputService")
local value = script.Parent:WaitForChild("CurrentPreview")
local pre = workspace.LadderStuff.Previews.PStage1
useri.InputBegan:Connect(function(input)
print("Input made")
if input.KeyCode == Enum.KeyCode.R then
print("Correct input")
if not pre == nil then
print("Part rotated")
pre.CFrame = pre.CFrame * CFrame.Angles(0, math.rad(20), 0)
else
print("Value nil")
end
end
end)
It always prints value nil and the value is not nil.
not takes precedence over the assigned to operator (==), so it’s basically evaluating not pre first, which will evaluate to false if it exists then compares false == nil, which of course is not equal, hence printing “value nil”.
The solution to this would be to use parantheses: if not (pre == nil), but it’s
better to use ~= (not equal to operator) because you’re “reinventing the wheel” (meaning you’re trying to create something that serves the same purpose in a different way than an already existing way)
local useri = game:GetService("UserInputService")
local value = script.Parent:WaitForChild("CurrentPreview")
local pre = value.Value
useri.InputBegan:Connect(function(input)
print("Input made")
if input.KeyCode == Enum.KeyCode.R then
print("Correct input")
if pre ~= nil then
print("Part rotated")
pre.CFrame = pre.CFrame * CFrame.Angles(0, math.rad(20), 0)
else
print("Value nil")
end
end
end)
You’re assigning pre to the objectvalue’s current value at the beginning of the script, which is kept constant. You’d want the current value at the time an input was made
One thing, since this is a placement system I have another script that loops the CFrame to the mouse position, and I cannot rotate it from another script because of that, how would I fix that?
this is the script that loops the cframe:
local tool = script.Parent
local previews = workspace.LadderStuff.Previews
local one = previews.PStage1
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local equipped
local value = script.Parent.CurrentPreview
tool.Equipped:Connect(function(mouse)
print("Weapon equipped")
one.Transparency = 0.7
value.Value = one
equipped = true
while wait(0.01) and equipped == true do
print("Loop")
local pos = (mouse.Hit.Position)
if value.Value == one then
one.CFrame = CFrame.new(pos + Vector3.new(0,6,0))
end
end
end)
tool.Unequipped:Connect(function(mouse)
print("Weapon unequipped")
equipped = false
one.Transparency = 1
end)
and this the one that tries to rotate it
local useri = game:GetService("UserInputService")
local value = script.Parent:WaitForChild("CurrentPreview")
useri.InputBegan:Connect(function(input)
print("Input made")
if input.KeyCode == Enum.KeyCode.R then
print("Correct input")
if value.Value ~= nil then
local pre = value.Value
if pre ~= nil then
print("Part rotated")
pre.CFrame = pre.CFrame * CFrame.Angles(0, math.rad(20), 0)
else
print("Value nil")
end
end
end
end)
Why do you have two scripts for the same placement system though? Well, for this you can make a value, preferably an IntValue, that will hold the rotation, and when you press R it’ll add to the rotation, then you apply the rotation in the script where you position it.