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

How would I have a script run in a certain area when a player walks into it?

I don't even know how to try to do this, I'm quite new to scripting and I want it to enable fog whenever someone enters a certain area. I'm assuming it has to do something with checking the player position but then I don't know what to do with it.

Below is some code I found to locate player position. I don't know how to utilize it though.

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		print(Character.HumanoidRootPart.Position) -- This would just print the position, I don't know how to make a script that would detect an area of coordinates
	end)
end)   

For toggling of fog, I think it would have to be something like

if Character.HumanoidRootPart.Position == --(area) then
	game.Lighting.Fog.Enabled = true

I just don't know how to make it detect an area other than listing every stud around the area (which would be excruciatingly painful)

1 answer

✓ Accepted answer

its quite easy actually and doesnt require position at all, there is an event called Touched which fires when one part touches another, you can use this event on a part that would cover the whole region to check parts that fires this event and see if they are a child of the player character

so first you need to create a new part and cover the region with it, make it transparent of course, second you need to insert a server script, named Script in studio and then we come to the actual code

script.Parent.Touched:Connect(function(part)--this event fires as soon as something starts to touch this part, but it doesnt keep firing while its being touched from the exact same place (if you stand still inside the part the event wont fire but if you start to move it will fire for every render the game makes
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)--this function takes the argument put inside the brackets and checks if its a players character, and if it is then it returns the player, otherwise it will return nil (nothing)
	if player then--so we can make sure its actually a player and not nil
		game.Lighting.Fog.Enabled = true
	end
end)

1 comment

0

Thank you so much!

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