How would I get all Players in game to Sit using a ClickDetector

Im trying to code a “Hyper Space” Button
That Forces All Players to Sit
This is the Code Im currently Working with

Players = game:GetService("Players")

script.Parent.ClickDetector.mouseClick:connect(function(plr)

Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do

print("yes")

end

end)

I got alot of it from the wiki since im not a great scripter, but how would I get the players in-game to sit when this button is pressed?

1 Like

You’re certainly on the right track. Your goal is to iterate through all the players, then you’ll want to run some logic on those characters.

local Players = game:GetService("Players")

script.Parent.ClickDetector.MouseClick:Connect(function (player)
    for n, Player in pairs(Players:GetPlayers()) do
        if Player.Character ~= nil and Player.Character:FindFirstChild("Humanoid") then
            Player.Character.Humanoid.Sit = true -- Or whatever you'd like
        end
    end
end)

How you take such an implementation further to suit your use case is completely up to you. Understanding fundamentally how this works is important, though, so you get a sense of how to achieve your endgame for a use case like this.

Inquiry: A button needs to force all players to sit.
Process:

  • You need to be able to iterate through all players in the first place
  • Use Players:GetPlayers() to get a table of all players
  • Ensure that they have the required components to perform your functionality in the first place - that means the existence of a Character and a Humanoid within that Character
    • This will ensure that if a Player doesn’t have a Character or a Humanoid, your function won’t throw an error and terminate your thread
  • Set the Sit property of the Humanoid to make them sit
  • This iteration will run through for all Players in that table returned from GetPlayers
2 Likes

Okay you win, that’s gotta be the most in depth answer I’ve seen for an issue like this lol. I’ll delete my response rq.

(In reference to aforementioned code that was removed) Fundamentally, this is correct when it answers OP, but in practice always be cautious of running statements on objects returned from FindFirstChild(OfClass). These functions can return nil values, thus if you’re not checking for the existence of such objects firsthand and they per chance return a nil, your code will throw an error. Something akin to trying to set a property for a nil value.

workspace:FindFirstChild("Base").Transparency = 1
--> If "Base" doesn't exist or isn't an object with a Transparency property, it'll error

local Base = workspace:FindFirstChild("Base")
if Base and Base:IsA("BasePart") then
    --> Confirming existence and proper class which has a Transparency property
    Base.Transparency = 1
end

You didn’t have to delete your post. It still answered the question, I was just in the middle of typing and didn’t stop lol. Normally I answer in as much depth as I can to help OP gain an understanding of implementation, though sometimes I’m not always right. :slightly_smiling_face:

1 Like

I used your script, it works perfectly, but then I run into a problem, The First person to join the server is the only one that is seated, even if another person pressed it, whats going wrong that is making it only 1 person sit?

Make sure that the script isn’t a LocalScript.

Its a normal script in a part in workspace

Just to make sure, you’re using the script above, correct?

Yes, if you want to see it in action its linked here

What is your implementation of the code? Did you modify it in any way or do you have any conflicting variables? I ran the exact code I posted in Roblox Studio in an Accurate Play Solo Local Server of 2 players - both players sat when the button was pressed. The fact that it only seats one player is odd behaviour, when that’s not what the specified code does.

This is the Actual Script

Players = game:GetService(“Players”)

script.Parent.ClickDetector.MouseClick:Connect(function (player)
for n, Player in pairs(Players:GetPlayers()) do
workspace.HyperSound:Play()
script.Parent.BrickColor = BrickColor.Red()

script.Parent.ClickDetector.MaxActivationDistance = 0

wait(4.5)

workspace.Hyper.ParticleEmitter.Enabled = true
workspace.HyperMain.ParticleEmitter.Enabled = true

wait(2)

if Player.Character ~= nil and Player.Character:FindFirstChild(“Humanoid”) then
Player.Character.Humanoid.Sit = true – Or whatever you’d like

wait(5)

workspace.Alien1.Transparency = 0
workspace.Alien2.Transparency = 0
workspace.Alien3.Transparency = 0

wait(1)

workspace.Hyper.ParticleEmitter.Enabled = false
workspace.HyperMain.ParticleEmitter.Enabled = false

wait(50)

workspace.Alien1.Transparency = 1
workspace.Alien2.Transparency = 1
workspace.Alien3.Transparency = 1

wait(900)
script.Parent.ClickDetector.MaxActivationDistance = 10
script.Parent.BrickColor = BrickColor.Green()
end
end
end)

Did you… per chance, check the Developer Console first, or review your code?

You’re missing an end statement here. The code is yielding for a long time because it’s still performing an operation on a single player. This is the cause for only one player experiencing effects.

local Players = game:GetService("Players")

script.Parent.ClickDetector.MouseClick:Connect(function (player)
    workspace.HyperSound:Play()
    script.Parent.BrickColor = BrickColor.Red()
    script.Parent.ClickDetector.MaxActivationDistance = 0

    wait(4.5)

    workspace.Hyper.ParticleEmitter.Enabled = true
    workspace.HyperMain.ParticleEmitter.Enabled = true

    wait(2)

    for n, Player in pairs(Players:GetPlayers()) do
        if Player.Character ~= nil and Player.Character:FindFirstChild(“Humanoid”) then
            Player.Character.Humanoid.Sit = true
        end
    end

    wait(5)

    workspace.Alien1.Transparency = 0
    workspace.Alien2.Transparency = 0
    workspace.Alien3.Transparency = 0

    wait(1)

    workspace.Hyper.ParticleEmitter.Enabled = false
    workspace.HyperMain.ParticleEmitter.Enabled = false

    wait(50)

    workspace.Alien1.Transparency = 1
    workspace.Alien2.Transparency = 1
    workspace.Alien3.Transparency = 1

    wait(900)

    script.Parent.ClickDetector.MaxActivationDistance = 10
    script.Parent.BrickColor = BrickColor.Green()
end)

I rewrote your code for you, since there were a little wonky placements of variables. Use this code and try to see if your problem is solved.

1 Like