Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to anchor player when TextButton clicked?

Hey peeps. So I've been having some trouble anchoring a player once the text button is clicked. I've tried using this script:

local part = script.Parent
local db = false

part.MouseButton1Click:Connect(function(clicked)
	local human = clicked.Parent:findFirstChild("Humanoid")
	if human then
		db = true
		human.Parent.UpperTorso.Anchored = true
		wait(20)
		human.Parent.UpperTorso.Anchored = false
	end
end)

But it doesn't work. The output says:

Players.Paradoa.PlayerGui.MainMenu.TextButton.LocalScript:5: attempt to index nil with 'Parent'

3 answers

So this is a common mistake with new scripters, but case does matter, and also don't forget to get your hierarchies correct.

So you don't even need this line but I do need to point out your error in it:

local humanoid = clicked.Parent:FindFirstChild("Humanoid") --first F in this function always must be capitalized

Your fixed script:

local part = script.Parent
local human = game.Players.LocalPlayer
local db = false

part.MouseButton1Clicked:Connect(function() --you need to make sure that you know what the event you choose to use returns something.
    if not db then --if you don't check for your debounce, then it won't work
        db = true
        human.UpperTorso.Anchored = true --human.Parent would've been workspace, so unless you have a part called UpperTorso inside the workspace, this wouldn't have worked.
        wait(20)
        human.UpperTorso.Anchored = false
        db = false
    end
end)

If this answers your question, then mark it as accepted, otherwise, let me know if you have any further questions about this.

2 comments

0

Why did you unaccept my answer? Do you need anything else?

0

I added more comments to explain better.

I'm not sure what the parameter for MouseButton1Click is, because I've never used it, and it doesn't show it on the wiki. However, I assume you're just trying to access the local player, so just use that.

local player = game:GetService("Players").LocalPlayer
local part = script.Parent
local db = false

part.MouseButton1Click:Connect(function()
    local human = player.Character:FindFirstChild("Humanoid") --note the capital F
    if human then
        db = true
        human.Parent.UpperTorso.Anchored = true
        wait(20)
        human.Parent.UpperTorso.Anchored = false
    end
end)

2 comments

1

Thank you

0

accept answer and upvote pl0x

GUIs have to be local scripts. If you use GUIs on a script, it will likely either do nothing or return nil. So you have to use Remote Events. Any mistakes and I will put them in --comments. Put the Remote Event in ReplicatedStorage.

Local Script

local textButton = script.Parent.TextButton --Put your local script BELOW the ScreenGui.
local db = false
local remoteEvent = game.ReplicatedStorage.RemoteEvent

textButton.MouseButton1Click:Connect(function() --You do not need the click parameter.
	if not db then --You do not need the humanoid as a player is required to click.
		db = true
		remoteEvent:FireServer()
		wait(20)
		--db = false --I assume you want it once, but remove the comment if you wanna do it multiple times.
	end
end)

Server Script

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player) --The player parameter has to be first, and we're sending the player.
	local character = player.Character
	character.HumanoidRootPart.Anchored = true --HumanoidRootPart is recommended.
	wait(20)
	character.HumanoidRootPart.Anchored = false
end)

1 comment

0

He is using a local script. Did you not read the error message? And I'm fairly certain that the local player's character is replicated, so you wouldn't have to use a remote event to get through filtering enabled.

This discussion is preserved as it was. New answers, comments, and votes are closed.