Players won't anchor?

Hello everyone, I hope your day is going well…

I need some help with scripting.
I inserted a localscript into the workspace and entered this script so that the player can’t move until they press the play button:

script.Parent.Player.LocalPlayer.Anchored = true until

script.Parent.PlayButton.MouseButton1Click:Connect(function() then

Player.LocalPlayer.Anchored = false

end)

I don’t know what’s wrong, because it won’t work…
Please help?
Thanks!

The fact is that you aren’t referencing a BasePart, nor referencing things correctly. I don’t know where the Player comes from and I believe you meant Players.LocalPlayer.Character as the correct reference. You need to anchor a part called HumanoidRootPart to freeze them, but it keeps them suspended in the air.

For alternative freezing, there is ContextActionService:


On another note, LocalScripts don’t function in workspace. You need to move them into a proper place, such as the player’s PlayerGui or the UI part.

5 Likes

Thank you. I was wondering if you could help with another problem related to the issue though?

The player stays anchored, until you press the close button on the customization thing I added, then the character can move freely…??

The close button is called BackButton and the play button is called PlayButton.

Do you meant by a toggle button? You can use a switch statement for this.

I’m sorry, but ive only been scripting for a month so i dont quite understand-

A togglable switch statement would look like this.

local switch = false
if not switch then
    switch = true
    - - do some stuffs here.
    wait(0.1)
else
    switch = false
    - - do some stuffs here
    wait(0.1)
end

 — NOTE: I add a wait() to act as a debounce. You can remove it if you want.
1 Like

Good to know-.

Idk, the script decides its going to work (but stop working when i press the close button on customization gui) and then the next minute it decides not to work at all!

With this, you can use ONE button. It will toggle whatever thing your trying to do.

1 Like

Okay so where do i put it? Inside the localscript?

sorry for being a nuisance, i know i am

So to answer your question. Here a script.

local plr = game.Players.LocalPlayer
local root = plr.Character.HumanoidRootPart or plr.Character.Torso

Attach this to both on the switch statement, just change it’s anchroed Boolean value.

You can put this under a local script and under a text button.

1 Like

Alright, i’ll keep you updated.

What does it mean by add some stuff here in your script? (this one: local switch = false
if not switch then
switch = true
- - do some stuffs here.
wait(0.1)
else
switch = false
- - do some stuffs here
wait(0.1)
end )

Sorry, I’m really dumb ;-;

That’s where you change the Boolean value of the anchored of the player’s root part/PrimaryPart of the model.

i feel so bad but

what’s a boolean :grimacing:

i’m sorry i am pretty inexperienced ;-;

Boolean value contains 2 values. True or false. Imagine a switch being turned on, that’s True. And now imagine it’s turned off, that’s a false. It’s very useful in if statements to check if you wanna only execute lines of codes when the condition is met. I suggest you to learn more of the basics of scripting, especially from AlvinBlox from YouTube. That’s where I mastered my basics of scripting. Hope these helps.

I made a script for you. Read the comments carefully to understand it. The script is located in StarterGui and it is LocalScript.

    local Players = game.Players -- Creating a variable for the Players service
    local character = Players.LocalPlayer.Character -- Getting access to Character.

    if not character or not character.Parent then -- Add the function if the Character did not have time to decide from the very beginning of the game.
    character = Players.LocalPlayer.CharacterAdded:wait()
    end

    
    local Button = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").TextButton -- define the button to click. In my case, this is a test button in my testGui, so add your own button here.

    character.HumanoidRootPart.Anchored = true 

    Button.MouseButton1Click:Connect(function() -- when you click the button, Anchor is disabled.
    if character.HumanoidRootPart.Anchored == true then
    character.HumanoidRootPart.Anchored = false
    end
    end)
2 Likes

Put the local script in PlayerGui. Also anchor the HumanoidRootPart and and wait for the character to load. That should fix it entirely. Local Scripts also are a little more complicated when in Workspace or in any area that isn’t the client.

where do you find the playergui in the explorer?

1 Like

It’s StarterGui. Although when the game loads everything in the StarterGui is loaded in the player called “PlayerGui.” My bad.

1 Like