generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Radius Checker

Previous Thread :: Next Thread 
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 03:31 PM
Since Roblox is being stubborn and their build in dialog system is refusing to work, I need to create my own. I want to make it detect if my character gets close to a block. Here's my script (localScript).

while true do
local player = game:GetService("Players").LocalPlayer
local radius = 5
local torsoPos = script.Parent.Position

if player:DistanceFromCharacter(Vector3.new(torsoPos)) < radius then
print("Player is within radius")
end
end

Thanks in advance.


#code "Thanks" -- Online
Report Abuse
0_1195 is not online. 0_1195
Joined: 21 Dec 2011
Total Posts: 268
06 Apr 2016 03:33 PM
There is no wait(). It will just crash.



\_(siggy)_/
Report Abuse
ghostleader is not online. ghostleader
Joined: 16 Feb 2008
Total Posts: 1131
06 Apr 2016 03:34 PM
TRy

while true do
wait(0.5)
local player = game:GetService("Players").LocalPlayer
local radius = 5
local torsoPos = script.Parent.Position

if (torsoPos - brickPos).magnitude < radius then
print("Player is within radius")
end
end
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
06 Apr 2016 03:40 PM
local player = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local torso = char:WaitForChild("Torso")
local radius = 5
local blockPlayerNeedsToBeNear = workspace:WaitForChild("Block")

while wait() do
if char and torso then
local torsoPos = torso.Position
if (blockPlayerNeedsToBeNear.Position - torsoPos).magnitude < 5 then
print("within radius")
end
end
end

because you don't need to re-define the player and radius each time you loop
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
06 Apr 2016 03:40 PM
You should use runservice instead of while true do
Report Abuse
ghostleader is not online. ghostleader
Joined: 16 Feb 2008
Total Posts: 1131
06 Apr 2016 03:43 PM
"You should use runservice instead of while true do"

Only if you want a fast rate, and in this case, OP should not check a ton.

while wait(1/5) do
would be better.

compared to using HeartBeat or Stepped.

For checking the distance, there is no reason to check every 1/30s,
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 04:29 PM
Still doesn't print


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 04:33 PM
Adding a wait doesn't change anything


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 04:50 PM
Bump


#code "Thanks" -- Online
Report Abuse
YourStoneMaster is not online. YourStoneMaster
Joined: 29 Mar 2013
Total Posts: 401
06 Apr 2016 06:02 PM
Here try this. torsoPos does not need to be in Vector3.new() since it is already a Vector3. Also MAKE SURE this is a local script inside your player. While true do always needs a wait.

while true do
local player = game:GetService("Players").LocalPlayer
local radius = 5
local torsoPos = script.Parent.Position
wait()
if player:DistanceFromCharacter(torsoPos) < radius then
print("Player is within radius")
end
end
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 06:30 PM
Sorry, that still doesn't work. I placed the local script in the StarterPack. Here's my current version.

while wait() do
local player = game.Players.LocalPlayer
local torsoPos = game.Workspace.HelperBot1.Torso.Position
local radius = 5

if player:DistanceFromCharacter(Vector3.new(torsoPos)) <= radius then
print("Player is within HelperBot1's radius.")
end
end


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 06:34 PM
What the heck? My script was disabled for some reason. Let me fix this.


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 06:50 PM
Script is no longer disabled, here's my new script.

#code print("Radius script is active")

while wait() do
local player = game.Players.LocalPlayer
local torsoPos = game.Workspace.HelperBot1.Torso.Position
local radius = 10

if player:DistanceFromCharacter(Vector3.new(torsoPos)) <= radius then
repeat
wait()
print("Player is within HelperBot1's radius.")
until player:DistanceFromCharacter(Vector3.new(torsoPos)) > radius
end
end


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 06:57 PM
Still doesn't work


#code "Thanks" -- Online
Report Abuse
PerilousDanger is not online. PerilousDanger
Joined: 21 Aug 2012
Total Posts: 3773
06 Apr 2016 06:59 PM
"it doesn't work" isn't that helpful

Which parts of it aren't working?

Does it crash?

Are there red lines in output?

Does it ever print anything?


step 2.

add in a bunch of print statements everywhere to see what's wrong with it
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:01 PM
Alright. The loop works fine, I tried putting a print in there. There's no errors in the output, and yes, it does print what I tell it to.


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:04 PM
Ah, I may be able to trace the problem. I put this in the loop:

print("Bot is ..." .. player:DistanceFromCharacter(Vector3.new(torsoPos)) .. " studs away.")

When I get closer to the bot, it says that the target is getting further away. Looking into it right now...


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:08 PM
Here, you can see the prints I added.

print("Radius script is active")

while wait() do
local player = game.Players.LocalPlayer
local torsoPos = game.Workspace.HelperBot1.Torso.Position
local radius = 10

print("Player is out of HelperBot1's radius, " .. player:DistanceFromCharacter(torsoPos) .. " studs away.")

if player:DistanceFromCharacter(Vector3.new(torsoPos)) <= radius then
repeat
print("Player is within HelperBot1's radius, " .. player:DistanceFromCharacter(torsoPos) .. " studs away.")
wait()
until player:DistanceFromCharacter(torsoPos) > radius
end
end

When the player gets within 10 studs of the Bot, it says in the output "Player is within HelperBot1's radius, x studs away." However, it only says this once, rather than until I leave it's radius. I'll keep filling you in.


#code "Thanks" -- Online
Report Abuse
PerilousDanger is not online. PerilousDanger
Joined: 21 Aug 2012
Total Posts: 3773
06 Apr 2016 07:13 PM
Hmm

You can always try .Magnitude if you want


local plr = game.Players.LocalPlayer
local char = plr.Character
local position = game.Workspace.HelperBot1.Torso.Position
local radius = 10

while wait() do
if (char:WaitForChild('Torso').Position - position).magnitude <= radius then
print(plr.Name, 'is in the radius')
end
end
Report Abuse
kools is not online. kools
Joined: 11 Jan 2009
Total Posts: 1659
06 Apr 2016 07:15 PM
The way I would approach this problem is creating an invisible 3d sphere with cancollide set to false. Then simply connect to the touched event. There's no need for this looping nonsense, right?
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:19 PM
Aha! @peri, it's working. Thanks for the help! And @kools, I'll look into that.


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:25 PM
@kools, no offence, but that would make it harder to detect whether it's in or out of the sphere's spectrum. Also, Roblox spheres have vertices, so it wouldn't be as precise.


#code "Thanks" -- Online
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:35 PM
local plr = game.Players.LocalPlayer
local char = plr.Character
local position = game.Workspace.HelperBot1.Torso.Position
local radius = 5

while wait() do
if (char:WaitForChild("Torso").Position - position).magnitude <= radius then
print(plr.Name .. " is in the radius")
plr.PlayerGui.BotChat.MainFrame.SlideIn.Disabled = false
plr.PlayerGui.BotChat.MainFrame.HelperBot1Active.Disabled = false
plr.PlayerGui.BotChat.MainFrame.BotName.PrepareText.Disabled = false
plr.PlayerGui.BotChat.MainFrame.ChatBox.PrepareText.Disabled = false
end

if (char:WaitForChild("Torso").Position - position).magnitude > radius then
print(plr.Name .. " is out of the radius")
plr.PlayerGui.BotChat.MainFrame.SlideOut.Disabled = false
end
end

This method is a bit spammy, but it works. Thanks to everyone who helped.


#code "Thanks" -- Online
Report Abuse
kools is not online. kools
Joined: 11 Jan 2009
Total Posts: 1659
06 Apr 2016 07:36 PM
Actually, instead of making it a sphere, make it a cube slightly larger than your radius. Then run your radius-checking bit in the event handler of touched. That way, we don't run a loop too often: which is always gross.
Report Abuse
OnlineOne is not online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
06 Apr 2016 07:37 PM
True, running a loop too often is gross, but what I said above about the sphere is still true. Also, the radius would be a square shape, rather than a circle.


#code "Thanks" -- Online
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image