Hi,
I made a sofa with a seat that has a proximity prompt and script attached to it. So that when player interacts with the prompt they get to sit on the sofa with a custom animation I made.
This was working a while ago and now when I suddenly checked it stopped working. I’ve been trying to figure out what it could be, but nothing.
Could someone have a look at this and let me know, please?
script:
local seat = script.Parent.Parent
local prompt = script.Parent
local sit = prompt.Parent
local sitAnim = script.Parent.Parent.sitanim
sit.Disabled = true
prompt.Triggered:Connect(function(player)
sit.Disabled = false
local char = workspace:WaitForChild(player.Name)
local hum = char:WaitForChild("Humanoid")
sit:Sit(hum)
prompt.Enabled = false
while sit.Occupant do
wait()
end
prompt.Enabled = true
sit.Disabled = true
end)
seat.Changed:Connect(function(property)
if property ~= "Occupant" then return end
local occupant = seat.Occupant
if occupant then
local character = occupant.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
sitAnim = humanoid:LoadAnimation(sitAnim)
sitAnim:Play()
end
elseif sitAnim then
sitAnim:Stop()
end
end)
The code looks just fine to me. I’m suspecting 3 things though.
- There are yielding warnings in our output.
- The humanoid you’re trying to get is keep on yielding and so the code doesn’t run.
- Add a
task.wait()
after sit.Disabled = false
to give it a slight time to register the change of property.
Also please use task.wait()
insead of wait
1 Like
Hey,
Thanks for your reply. There are sadly no warnings in output. I did add the task wait and changed that to such, but sadly nothing.
One thing I noticed in your code is that you are using while
loop inside the triggered event. Never do that! It puts a heavy toll on the memory and performance. If someone triggers it so fast that the code couldn’t register it, multiple loops will be triggered which will ultimately create unnecessary loops.
Also, why use the while loops? I didn’t understand why you used it here, seems unnecessary to me.
Imo, do this instead:
prompt.Triggered:Connect(function(player)
sit.Disabled = false
local char = player.Character or player.Character:Wait()
local hum = char:WaitForChild("Humanoid")
sit:Sit(hum)
prompt.Enabled = false
--[[ What are you trying to do here? This is unnecessary.
while sit.Occupant do
wait()
end
prompt.Enabled = true
sit.Disabled = true
]]
end)
Hey!
I added that option as a fail safe to prevent the spamming of the prompt, but I’ll remove it then. I implemented the code you suggested, but once my character jumps off the seat, the prompt does not show up again to let me sit again or someone else.
I somehow also can sit on the seat without having to press the prompt which defeats the purpose of the prompt.
I’m still somewhat struggling with the code, something so simple won’t seem to work. Whenever my character jumps off the seat and then sits on it again using the prompt the animation doesn’t play. It does play on the first time sitting on it:
local seat = script.Parent.Parent
local prompt = script.Parent
local sit = prompt.Parent
local sitAnim = script.Parent.Parent.sitanim
sit.Disabled = true
prompt.Triggered:Connect(function(player)
sit.Disabled = false
local char = player.Character or player.Character:Wait()
local hum = char:WaitForChild("Humanoid")
sit:Sit(hum)
prompt.Enabled = false
end)
seat.Changed:Connect(function(property)
if property ~= "Occupant" then return end
local occupant = seat.Occupant
if occupant then
local character = occupant.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
sitAnim = humanoid:LoadAnimation(sitAnim)
sitAnim:Play()
end
elseif sitAnim then
sitAnim:Stop()
prompt.Enabled = true
end
end)