How do I make this function work when the value is chair2 and when it changes to chair2 ?
What value type is CN here though?
CN is a StringValue for the chair 2
And what do you mean by “How do I make this function work when the value is chair2 and when it changes to chair2 ?”?
CN.Changed:Connect(function(newVal)
if newVal == "Chair2" then
--do code
end
end)
I think this is what you’re asking for, it’s hard to tell.
local Parent1 = script.Parent
local CN = workspace.Guns.Gun.ChairNumber
Parent1.ChildAdded:Connect(function(child)
if child.Name == "SeatWeld" then
--do code
end
end)
CN.Changed:Connect(function(newVal)
if newVal == "Chair2" then
--do code
end
end)
local Parent1 = script.Parent
local CN = workspace.Guns.Gun.ChairNumber
Parent1.ChildAdded:Connect(function(child)
CN.Changed:Connect(function(newVal)
if newVal == "Chair2" then
--do code
end
end)
if child.Name == "SeatWeld" then
--do code
end
end)
2 slightly different implementations depending on what you need.
I’m making a chair that when you sit down, it runs a function if the value of CN is chair2 but if you sit in the chair and the value is not chair2, I don’t know how to make the function run if the player is already seated.
Oh, that’s relatively simple, thanks for explaining it better:
local seat = script.Parent
local CN = workspace.Guns.Gun.ChairNumber
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant then
if CN.Value == "Chair2" then
--do code
end
end
end)
Just place the script inside the seat itself, so like this:
Thanks! but imagine that a player sits but not yet the value of CN is not chair2 so the function does not run but if the player is sitting and the value of CN changes to chair2 it does not run the function
My problem is that if the player sits first that the value of CN is chair2 I want the function to run (Thanks for helping me <3)
local seat = script.Parent
local CN = workspace.Guns.Gun.ChairNumber
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant then
CN.Changed:Connect(function(newVal)
if newVal == "Chair2" then
--do code
end
end)
end
end)
And that should do the trick.
I’ve been trying to do that for days, thank you very much <3