Currently working on a classic game, right now I’m making a garage door. I want the house to be rented out to people, so once I rent the house to them, only they can open the door.
(The garage door is a model from a friend)
Here’s the code:
if player.UserID = 994454489 then
local Handle = script.Parent.Handle
local open = false
local active = false
function click()
if open == false and active == false then
active = true
for i = 1,60 do
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(1.5)))
wait()
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(0.05,0,0))
wait()
end
active = false
open = true
elseif open == true and active == false then
active = true
for i = 1,60 do
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(-1.5)))
wait()
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(-0.05,0,0))
wait()
end
active = false
open = false
end
end
Handle.ClickDetector.MouseClick:connect(click)
I’m a total doofus at scripting, so I just tried to make that work, but of course it doesn’t!
It’s most likely a beginners mistake, but I will still be thankful for any tips.
So my way of doing it (if it was correct) wouldn’t work?
if player.UserID = 994454489 then
local Handle = script.Parent.Handle
local open = false
local active = false
function click()
if open == false and active == false then
active = true
for i = 1,60 do
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(1.5)))
wait()
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(0.05,0,0))
wait()
end
active = false
open = true
elseif open == true and active == false then
active = true
for i = 1,60 do
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(-1.5)))
wait()
script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(-0.05,0,0))
wait()
end
active = false
open = false
end
end
local enabledDoor = false
local function onClick()
if (enabledDoor) then
enabledDoor = false
-- code to disable the door state
else
enabledDoor = true
-- code the enable the door state
end
end
You create a function to detect when the ‘button’ is clicked, if the door is enabled then disable it, and vias versa.
Edit: Also I noticed that you’re using :SetPrimaryPartCFrame() but you should use :PivotTo(). :SetPrimaryPartCFrame() has float point errors and will slowly make the parts parented to the model move away by a very small amount, while :PivotTo() doesn’t do that and works long term perfectly (if the documentation stated it correctly).
local clickDetector = -- Path to you ClickDetector
local playerCanClick = -- Path to the player who can click the ClickDetector
clickDetector.MouseClick:Connect(function(playerWhoClicked)
if playerCanClick == playerWhoClicked then
-- Insert the code you want the script to execute here
end
end)
However you should add a connection for efficiency so something like this:
local clickDetector = -- Path to you ClickDetector
local playerCanClick = -- Path to the player who can click the ClickDetector
local connection
local function click(playerWhoClicked)
if playerCanClick == playerWhoClicked then
-- Insert the code you want the script to execute here
-- In the end of the code you put here, do this:
connection:Disconnect()
end
end)
connection = clickDetector.MouseClick:Connect(click)
Oh, so “playerWhoClicked” should be the username of the user I want to be able to open the door? Sorry for the tons of questions, I really am a doofus at scripting.
No, but if you want to put the player user id which will be the most accurate do this:
local clickDetector = -- Path to your ClickDetector
local playerCanClick = -- Path to the player who can click
clickDetector.MouseClick:Connect(function(playerWhoClicked)
if playerCanClick.UserId == playerWhoClicked then
print("Success!")
end
end)
If you put the playerName in playerWhoClicked if 2 players have the same name it will allow both players to click the part.
If you still don’t want to do the code I put above do this:
local clickDetector = -- Path to you ClickDetector
local playerCanClick = -- Path to the player who can click the ClickDetector
local connection
local function click(playerWhoClicked)
if playerCanClick == playerWhoClicked then
-- Insert the code you want the script to execute here
-- In the end of the code you put here, do this:
connection:Disconnect()
end
end)
connection = clickDetector.MouseClick:Connect(click)
I recommend you use the 2nd code block I gave since it also removes the function from memory when it is not needed!
Even if they can’t be the same they should not be used. UserId is meant and safer for this, just in case Roblox decides to change .Name and multiple users can have the same name the code will glitch