I honestly don’t know what’s going on. I made a local script that sends LocalPlayer through a RemoteEvent. However, when getting the position of the HumanoidRootPart for the magnitude, I get an error: Position is not a valid member of Position
The code that I use is this (a script in ServerScriptService):
copyKeyGUI.OnServerEvent:Connect(function(player, item)
local GUIPresent = player.PlayerGui:FindFirstChild("keyGUI")
local magnitude = (item.Position - player.Character.HumanoidRootPart.Position).Magnitude
if (magnitude < distance) and GUIPresent.Adornee ~= item then
local GUI = keyGUI:Clone()
GUI.Adornee = item
GUI.Parent = player.PlayerGui
end
end)
I’m certain that it has nothing to do with my LocalScript (hopefully) because when I do print(player) on the first line of the function, it prints out my username. I hope you could help me out with this!
Item is what’s returned on this on the LocalScript:
function searchTool()
for i, v in pairs(itemlist:GetChildren()) do
local magnitude = (v.Position - Player.Character.HumanoidRootPart.Position).Magnitude
if magnitude < distance then
return v
end
end
end
item = searchTool() etc etc.
I’m trying to recheck if the player is within reach to avoid any problems.
I haven’t checked if item that is being sent to the script is not problematic. I will have to check on that now.
This might take me a while since I haven’t really studied ipairs but I’ll give it a try
itemlist is a folder that contains a part which is supposed to be the item being sent into the server-side script. However, my username came out instead of the part inside the folder
Here’s my full script since it isn’t that long yet so yeah:
LocalScript (located in StarterGui)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local keyGUI = ReplicatedStorage.GUI.keyGUI
local copyKeyGUI = ReplicatedStorage.Events.copyKeyGUI
local destroyKeyGUI = ReplicatedStorage.Events.destroyKeyGUI
local itemlist = workspace.OBJ
local distance = 15
function searchTool()
for i, v in pairs(itemlist:GetChildren()) do -- change later
local magnitude = (v.Position - Player.Character.HumanoidRootPart.Position).Magnitude
if magnitude < distance then
return v
end
end
end
while wait(0.1) do
local item = searchTool()
if item then
local checkGUI = item:FindFirstChild("keyGUI")
local char = Player.Character
local magnitude = (item.Position - char.HumanoidRootPart.Position).Magnitude
if (magnitude < distance) and not checkGUI and char then
copyKeyGUI:FireServer(Player, item)
else
local GUI = Player.PlayerGui:FindFirstChild("keyGUI")
if GUI and checkGUI and char then
destroyKeyGUI:FireServer(Player, item)
end
end
end
end
Script (located in ServerScriptService):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local copyKeyGUI = ReplicatedStorage.Events.copyKeyGUI
local destroyKeyGUI = ReplicatedStorage.Events.destroyKeyGUI
local keyGUI = ReplicatedStorage.GUI.keyGUI
local distance = 15
copyKeyGUI.OnServerEvent:Connect(function(player, item)
print(item)
print(player)
local GUIPresent = player.PlayerGui:FindFirstChild("keyGUI")
local magnitude = (item.Position - player.Character.HumanoidRootPart.Position).Magnitude
if (magnitude < distance) and GUIPresent.Adornee ~= item then
print("detected")
local GUI = keyGUI:Clone()
GUI.Adornee = item
GUI.Parent = player.PlayerGui
end
end)
destroyKeyGUI.OnServerEvent:Connect(function()
print("destroyed")
end)
This is correct. When Fire()ing a remote event to the server, the local player parameter is added by default before any other parameters. If you do as Syclya said, your problem should be solved.