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

the brick-color is not changing when i fire this function?

So, In this case I'm using ContextActionService, it's similar to UserInputService, but the benefits of ContextActionService is including mobile buttons.

What I'm trying to achieve is making the button appear when I'm close to the block. Everything works fine, but the brick color is adjusting every second?

I think it's because i have a while loop, but it does not work if i remove the while loop.

Here's the local script:

local ContextActionService = game:GetService("ContextActionService")
local AggroMag = 50
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 wait(1) do
if (Torso.Position - Part.Position).magnitude <= AggroMag then
ContextActionService:BindAction("turnBrickYellow", OnPress, true, "t")
   else
	   ContextActionService:UnbindAction("turnBrickYellow")
	end
	end

1 answer

✓ Accepted answer

I'm bad at explaining things, so I won't go into detail. What I did is i've added Debounce to make sure it doesn't loop once you get near the part.

local plr = game:GetService('Players').LocalPlayer
repeat wait() until plr and plr.Character and plr.Character:FindFirstChild('HumanoidRootPart')
local char = plr.Character

local ContextActionService = game:GetService("ContextActionService")

local Part = workspace.Part
local Torso = char.HumanoidRootPart

function OnPress()    
    Part.BrickColor = BrickColor.Random()
end

local AggroMag = 50
local nearPart = false
while wait() do
	if (Torso.Position-Part.Position).Magnitude <= AggroMag and nearPart == false then
		nearPart = true
		ContextActionService:BindAction("turnBrickYellow", OnPress, true, "t")
	elseif nearPart == true then
		nearPart = false
		ContextActionService:UnbindAction("turnBrickYellow")
	end
end

2 comments

0

Oh, nice let me check it!

0

It works, but the IO's version the button is flickering.

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