I'm trying to make a first person shooting game with my friends. I need a gui that opens if player press "y" and his cursor is on another player. I'm trying to make a invite to team gui in my game. I wrote this code in a localscript:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local Players = game:GetService("Players") local workspace_l = game:GetService("Workspace") local selection = Instance.new("SelectionBox") selection.Color3 = Color3.fromRGB(255, 0, 0) selection.Parent = script mouse.KeyDown:Connect(function(key) if key == "Y" or key == "y" then local target = workspace:FindPartOnRay(Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction*50000)) if not target then selection.Adornee = nil else if target.Parent:IsA("Model") then local findhumanoid = target.Parent:FindFirstChild ("Humanoid") if findhumanoid ~= nil then local findplayer = game.Players:FindFirstChild (findhumanoid.Parent.Name) if findplayer ~= nil then if findplayer ~= player then local otherpos = findplayer.Character.HumanoidRootPart.Position local frame = script.Parent.InviteMenu.Frame local loop = true if player.isLeader.Value ~= false then if Players[tostring(findplayer)].IsAMemberOf.Value == "Players" then repeat wait() if player:DistanceFromCharacter(Vector3.new(otherpos)) <= 20 then if frame.Visible ~= true then local userid = findplayer.UserId local thumbType = Enum.ThumbnailType.HeadShot local thumbSize = Enum.ThumbnailSize.Size100x100 local content, isReady = Players:GetUserThumbnailAsync(userid, thumbType, thumbSize) frame.ImageLabel.Image = content frame.ImageLabel.Size = UDim2.new(0, 100, 0, 100) frame.target.Value = tostring(findplayer) frame.Visible = true frame.invite.MouseButton1Click:Connect(function() wait() loop = false end) wait() end else loop = false frame.Visible = false wait() end until loop == false end end end end end end end end end)
It works only for some players or some parts of players. What is my mistake? What can I do to make it work?