Why is my unanchor locally ontouch script not working?

Hello!
I’m trying to make it so that if a player touches a part, it will unanchor only on the player’s client.
My scripts do just about that, but It doesn’t work.
What is wrong?
Error: Players.SinClosed.PlayerGui.Unanchor:2: attempt to index nil with ‘Anchored’

Script:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local rep = game.ReplicatedStorage
		local event = rep.LocalUnanchor
		local part = script.Parent
		event:FireClient(player, part)
	end
end)

LocalScript (Inside StarterGui)

game.ReplicatedStorage.LocalUnanchor.OnClientEvent:Connect(function(plr, Part)
	Part.Anchored = false
end)

Can someone help me? Thanks!

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local part = script.Parent
local event = ReplicatedStorage:WaitForChild("LocalUnanchor")

part.Touched:Connect(function(hit)
	if (hit.Parent:FindFirstChild("Humanoid")) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if (player) then
			event:FireClient(player, part)
		end
	end
end)

Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("LocalUnanchor")

event.OnClientEvent:Connect(function(part)
	part.Anchored = false
end)

One of the main problems was you had the first parameter set to the player in the client event, as you did not need to do that! It is also better to use :WaitForChild() to make sure it gets the RemoteEvent because it takes a bit of time for instances to load! You should really use :GetService() GetService does a classname look up and ensures you get the service you asked for. Using the dot operator gives an object by name, not by classname, so if you rename ReplicatedStorage to RepStore to give an example, the dot operator will error, if you do game

If this works please set as solution :smiley:

It didnt work, same error as always!

What was the error? Can you please send me a screenshot

Players.SinClosed.PlayerGui.Unanchor:5: attempt to index nil with ‘Anchored’

Nevermind. I saw that is the entire script.
Could you send the entirety of the script? (Your script called “Unanchor”)

Are you sure the part is found on the server? It might not be found for some reason.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("LocalUnanchor")

event.OnClientEvent:Connect(function(part)
	part.Anchored = false
end)

wdym? its on workspace but i want it to unanchor when the player touches on client

Are you sure the part is a… part? because i just tried it in the studio and it worked fine for me? can you send a screenshot of it in the explorer?

Hmm… If you print the Part before you send it to the client and before you set it’s anchor on the client.

Do both print out the name of your part? (“UnanchorBlock”)

what do you mean? idk what u mean

Do the following:

-- Server
... -- What ever your current server code is before the next line
print(part)
event:FireClient(player, part)
... -- Whatever your current server code is after the previous line
-- Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("LocalUnanchor")

event.OnClientEvent:Connect(function(part)
	print(part)
	part.Anchored = false
end)

I did that and now this is randomly working! Thanks!

-_- bro your thats not what fixed it…

nvm i should give it to pomatthew the longer you stay on the part it gets unanchored

1 Like

It’s to late you already set the solution to his :’(

No. Yours is marked as the solution.

However, could you please add an explanation to why your code solves their code?

While I understand it was because they tried to receive the plr on the client side, other who have this might not.

It will be very helpful for them to see this reasoning on the solution rather on the bottom of the thread.

i edited it into the solution! check it out

1 Like