Hey, im making a inventory system and Im trying to get a gui to show that your near a part. But for some reason it only works for one part in the folder. Please help!
local userInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character
repeat wait() character = player.Character until character
local inDistance = false
game:GetService("RunService").RenderStepped:Connect(function()
for _, object in ipairs(game:GetService("Workspace").Interactables:GetChildren()) do
local humanoidRootPart = character["HumanoidRootPart"]
local basePart = object or object.PrimaryPart
local distance = math.floor((humanoidRootPart.Position - basePart.Position).Magnitude)
--print(distance.." STUDS AWAY")
if distance < 5 then
script.Parent.Visible = true
inDistance = true
else
script.Parent.Visible = false
inDistance = false
end
end
end)
I never use math.floor with Magnitude, try using this. If this don’t work try increasing the distance to 10 or something.
local player = game:GetService("Players").LocalPlayer
local character
repeat wait() character = player.Character until character
local inDistance = false
game:GetService("RunService").RenderStepped:Connect(function()
for _, object in ipairs(game:GetService("Workspace").Interactables:GetChildren()) do
local humanoidRootPart = character["HumanoidRootPart"]
local basePart = object or object.PrimaryPart
local distance = (humanoidRootPart.Position - basePart.Position).Magnitude
--print(distance.." STUDS AWAY")
if distance < 5 then
script.Parent.Visible = true
inDistance = true
else
script.Parent.Visible = false
inDistance = false
end
end
end)
local player = game:GetService("Players").LocalPlayer
local character
repeat wait() character = player.Character until character
local inDistance = false
game:GetService("RunService").RenderStepped:Connect(function()
for _, object in ipairs(game:GetService("Workspace").Interactables:GetChildren()) do
local humanoidRootPart = character["HumanoidRootPart"]
local basePart = object or object.PrimaryPart
local distance = (humanoidRootPart.Position - basePart.Position).Magnitude
--print(distance.." STUDS AWAY")
if distance < 5 then
script.Parent.Visible = true
inDistance = true
break
else
script.Parent.Visible = false
inDistance = false
end
end
end)
Try this, Maybe this would work.
As what i think, the issue is that When the Script makes the GUI Visible, It still continues checking
the Distance between you and Other Parts, Which causes it to become Not Visible.
I Know i am bad at explaining, but give my script a try.
Small nitpick, but this doesn’t work when the character dies.
A really simple fix for this is:
local character = player.Character or player.CharacterAdded:Wait()
player.CharacterAdded:Connect(function()
character = player.Character
end)
player.CharacterRemoving:Connect(function()
character = nil
end)
game:GetService("RunService").RenderStepped:Connect(function()
if not character then return end
-- do all that stuff
end)