The closer the player gets to an object, the more red it becomes

hi, it’s 3 days i’m working on this effect, but it just seems not to work

  1. What do you want to achieve? i’m trying to make a system where the player must understand that the object they are getting close to is dangerous. The object is going to be white, and the closer the player gets to it, the redder the block becomes

  2. What is the issue? i just don’t know how to make it work

  3. What solutions have you tried so far? i tried looking for solutions on the forum, and using magnitude, but the effect I get is that when the player approaches the block suddenly turns red, while what I want to achieve is that the closer the player is, the more red the block is, so this also includes intermediate colors between the initial white and red

please, any answer is appreciated, and could be the one to help me, i’m really struggling at this

3 Likes

Hopefully this math is correct:

local player = game.Players.LocalPlayer
local part -- fill in variable
local eventrange = 20
local originalcolor = part.Color

game:GetService("RunService").RenderStepped:Connect(function()
	local rootpos = player.Character.HumanoidRootPart.Position
	part.Color = originalcolor:Lerp(Color3.new(1,0,0), 
		1 - math.clamp((rootpos - part.Position).Magnitude / eventrange), 0, 1)
end)
2 Likes

If its turning red suddenly you just need to scale the values:

local whiteDistance = 100 --Part completely white at this distance
local redDistance = 20 --Part completely red at this distance
local whitenes = math.clamp((distance - redDistance) / whiteDistance, 0.0, 1.0)
block.Color3 = Color3.new(1.0, whitenes , whitenes) --When whiteness is lower the part looks redder 

image

1 Like

thanks, i’m gonna try it when i get home (2h max) in the meanwhile, would you tell me where to put the local script please? i read something online and i know it can be placed in different services, where would this one go to?

It can be put anywhere, as long as the local script will run. But you should put it somewhere relavent, like where the red object is handled.

what does ‘distance’ stand for?

distance is whatever distance you want to use for the effect, so you could take the difference between the characters position and the part position.

it sadly doesn’t work, any other suggestions?

How does it not work? What is wrong?

i don’t know. i did put the local script in StarterPlayerScript, as i read it can be there, this is the script.

local player = game.Players.LocalPlayer

local part = game.Workspace.Part -- fill in variable

local eventrange = 37

local originalcolor = part.Color

game:GetService("RunService").RenderStepped:Connect(function()

local rootpos = player.Character.HumanoidRootPart.Position

part.Color = originalcolor:Lerp(Color3.new(1,0,0),

1 - math.clamp((rootpos - part.Position).Magnitude / eventrange), 0, 1)

end)

when i join the game to test nothing happens

Schermata 2022-12-02 alle 17.44.43

have you tested it?

i finally solved the problem myself

local player = script.Parent
local part = game.Workspace.Part -- fill in variable
local whiteDistance = 225 --Part completely white at this distance
local redDistance = 0.01 --Part completely red at this distance
local waitTime = 0.05 -- set this to the delay time between each time you check the distance
while task.wait(waitTime) do
	if player then
		local distance = (player.HumanoidRootPart.Position - part.Position).Magnitude
		local whitenes = math.clamp((distance - redDistance) / whiteDistance, 0.0, 1.0)
		part.Color = Color3.new(0.6, whitenes , whitenes) --When whiteness is lower the part looks redder
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.