Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I made a script that detects if the player is within a certain radius but?... [Please help.. :( ]

So, i created a script using ContextActionService; ContextActionService is a service similar to UserInputService, but has buttons that work on iphones, and ipads.

What I'm trying to achieve is when i'm close to the block it shows the button, that i mentioned earlier and when you press it, it triggers the function.

My problem is that it's firing the function even though i have not pressed the button yet. Advanced help needed.

If you want to please do, since i have a problem with procrastinating.

Here's the script:

local ContextActionService = game:GetService("ContextActionService")

local AggroMag = 100

wait(2)
local Character = game.Players.LocalPlayer.Character
local Torso = Character:FindFirstChild("HumanoidRootPart")

local Part = game.Workspace.Part
local function OnPress()
	
	Part.BrickColor = BrickColor.Random()
end

while true do
wait(1)
if (Torso.Position - Part.Position).magnitude <= AggroMag then
ContextActionService:BindAction("turnBrickYellow", OnPress, true, "t")
ContextActionService:SetPosition("turnBrickYellow", UDim2.new(0.72,-25,0.20,-25))
   else
	   ContextActionService:UnbindAction("turnBrickYellow")
end
end

1 answer

well you are not looping the while true do when you say that you would need to put a wait() at the end of the loop

	while true do
	wait(1)
	if (Torso.Position - Part.Position).magnitude <= AggroMag then
	ContextActionService:BindAction("turnBrickYellow", OnPress, true, "t")
	ContextActionService:SetPosition("turnBrickYellow", UDim2.new(0.72,-25,0.20,-25))
   else
	       ContextActionService:UnbindAction("turnBrickYellow")
		
	end
	wait(0.5)
  end

or you could try to make the magnitude as a variable and then make it

	if variableName < AggroMag then

	end

if not, let me know

This discussion is preserved as it was. New answers, comments, and votes are closed.