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

How do I make a part visible once it reaches certain distance from player?

Hello. I'm trying to make a game based on tornadoes, and I used particle effects to create a realistic looking tornado. The only downfall with this is that particle effects are only visible within a certain short distance. What I am trying to do is make the base part for the tornado visible to a specific player once they get to far to see the particle effects, and invisible again when they can be seen. I know that I would have to work with a local script to do this, but I can't figure out anything. Any ideas?

2 answers

Put this in a local script in StarterPlayerScripts.

local myself = game.Players.LocalPlayer
local distanceToSeeTornado = 100 --Change this to whatever distance.
local gameStarted = true --Putting this in if you wanted a minigame sorta game. Make sure to modify this variable and set it accordingly to your minigame setup.

while true do
	if gameStarted then
		local tornado = workspace:FindFirstChild("TornadoPart") --Rename tornado part to whatever the base part is supposed to be named.
		if myself:DistanceFromCharacter(tornado.Position) <= distanceToSeeTornado then
			tornado.Transparency = 0  --Alternatively, you could replace 0 with 1 - (distanceToSeeTornado - myself:DistanceFromCharacter(tornado.Position))/100, though I'm not fully sure if that'll work.
		else
			tornado.Transparency = 1
		end
	end
	wait()
end

If you don't need the minigame structure, just remove the gameStarted Variable and the 'if statement' used with it.

4 comments

0

I've configured the script any way I can, for some reason it just doesn't seem to work.

0

What exactly did you change? If you changed the Transparency thing to my alternative, then it wouldn't have worked, because I am an idiot and I forgot that transparency only goes up to 1. I edited this accordingly, so use the updated script. Did you make sure gameStarted is true?

0

I deleted the game start feature, because I didn't need it. I tried the alternative, and realized immediately it wouldn't work. I also tried changing the method of player tracking, it didn't work. Method of directories, functions, you name it. The script just wouldn't work, but it wouldn't display any errors either.

0

Try the updated script, with the variable set true and everything. Make sure the funnel is named TornadoPart.

I have managed to create my own script. It still doesn't work, but maybe this could give a good start to anyone trying to answer.

local funnel = script.Parent --Directory to base tornado part

game.Players.PlayerAdded:Connect(function(player) --Starts when a player joines the game
	local Player = game:GetService("Players").LocalPlayer.Name
	local Character = workspace:FindFirstChild(Player)
	while true do
		if Character.UpperTorso then --Uses r15 characters
			local magnitude = (Character.UpperTorso.Position - workspace.Tornado.Funnel.Position).Magnitude --Uses magnitude instead of "DistanceToPlayer"
			if magnitude > 600 then --Distance I want tornado to be visible past
				funnel.Transparency = 0
			else
				funnel.Transparency = 1
			end
			wait(1) --There are lot's of other things going on, so secondly updating would be better
		end
	end
end) 

1 comment

0

I'm assuming this is a local script, because of the LocalPlayer on line 4. However, because of the first line (stating that the funnel is the script's Parent), this wouldn't work unfortunately :(. LocalScripts only work on players.

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