✓ Accepted answer
First of all, you should probably make variables’ names more than just one letter, it will be pretty hard to figure out what they mean later on.
Now, instead of waiting until the button is touched, you can connect a .Touched event to it. This means what whenever the button is touched, the event will fire. The event can fire multiple times, which means loops are not needed.
Then we check if the thing that touched the button is actually a player. Check if the part’s parent touching it has a Humanoid. However only do that if your game does not contain AIs with humanoid, such as zombies. This is done with :FindFirstChild(), it returns nil if the instance does not exist.
If your game does contain things with humanoid other than the player, simply check if the part’s parent’s name is in the player list.
Another thing is, you can probably use Mover Constraints / Body Movers to move your platform, however I’m not really good with those so I won’t try to explain those, instead I’ll just link the articles about them.
You could use TweenService instead, but keep in mind that the player will slide off the platform when the tween is going, this does not occur with Mover Constraints / Body Movers.
button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
end
end)
local Players = game:GetService('Players')
button.Touched:Connect(function(hit)
if Players:FindFirstChild(hit.Parent.Name) then
end
end)
Mover Constraints: https://developer.roblox.com/en-us/articles/constraint-movers
BodyMover: https://developer.roblox.com/en-us/api-reference/class/BodyMover
^This one is deprecated though, so Mover Constraints is preferred
2 comments
thank you, when i put in your code it works now.
If it solved your problem, make sure to mark it as the solution. Plus, you should also take a look at the other answer, they offered some great advise as well