So basically i’m trying to make a script where only a certain player can sit in a chair.
Basically the script tries to see if the player name matches the username variable and if it does the player should be able to sit. Basically in the variable it gives a path to a textlabel containing a player’s username.
For some reason this script won’t work, no errors or anything.
local username = script.Parent.Parent.Parent.owner.bill.label.Text
seat.Disabled = true
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local humanoid = seat.Occupant if not humanoid then return end
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
if player.humanoid.Name == username then
seat.Disabled = false
else
seat.Disabled = true
end
end)
I don’t think one is less efficient than the other. It would be less efficient (in implementation) to check if an “authorized” player is trying to sit on it to enable it again.
local Players = game:GetService("Players")
local username = script.Parent.Parent.Parent.owner.bill.label
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local humanoid = seat.Occupant
if not humanoid then
return
end
local player = Players:GetPlayerFromCharacter(humanoid.Parent)
if player.Name ~= username.Text then
humanoid.Jump = true
end
end)
Your username variable would not update with the text property. So if the text changes the variable won’t.
I would avoid using GetPropertyChangedSignal. Instead monitor the SeatWeld that appears in the seat when a player sits down.
Both of the following worked for me:
seat.ChildAdded:Connect(function(child)
if child.Name == "SeatWeld" then
local playerName = seat.Occupant.Parent.Name
-- Do whatever with the player's name
end
end)
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant then
local playerName = seat.Occupant.Parent.Name
-- Do whatever with the player's name
end
end)
Make sure the seat is enabled to begin with. Also make sure this is all in a Script (not a LocalScript).
It seems what I added doesn’t work, I’m not quite sure what’s wrong with this.
local username = script.Parent.Parent.Parent.owner.bill:WaitForChild("label").Text
seat.Disabled = true
seat.ChildAdded:Connect(function(child)
if child.Name == "SeatWeld" then
local playerName = seat.Occupant.Parent.Name
print(1)
if username == playerName then
seat.Disabled = false
wait(0.5)
end
end
end)
The problem is that your seat is disabled. Like I said in my post, make sure that the seat is enabled to begin with. Right at the top of your script, you are disabling the seat, so the ChildAdded function will never be called.
The script seems to work but now I seem to have a new problem, the script detecting the user doesn’t work.
local username = script.Parent.Parent.Parent.owner.bill:WaitForChild("label").Text
seat.Disabled = false
seat.ChildAdded:Connect(function(child)
if child.Name == "SeatWeld" then
local playerName = seat.Occupant.Parent.Name
print("HEY LISTEN!")
if username == playerName then
-- ^^ checks a textlabel to see if the playername matches the name in the textlabel.
print("how")
wait(0.5)
else
print("itbe")
wait(0.5)
end
end
end)
Seats create welds, to prevent someone sitting, just destroy the weld that’s created by the seat.
e.g.
if true then --if a certain condition is true then
Seat.SeatWeld:Destroy()
end
Note: There’s nothing wrong with using :GetPropertyChangedSignal in this situation (in my opinion), it might even consume less resources considering you’re checking whether a child has been added then doing another check to see if a SeatWeld has been added.