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