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.
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.
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!
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 )
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)
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.