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

Using a region script to make the camera shake in a specific area that I want?

I have had luck getting the camera to shake the way I want, thanks to you guys for helping. But the script to get it to run in a certain area is not working. Here is what I have

script

function shake()
	local players = game.Players:GetPlayers()
	for i=1, #players do
		local character = players[i].Character
		local shakescript = character:FindFirstChild("ShakeScript")
		if shakescript ~= nil then
			shakescript.Disabled = false
		end
	end
end

script adder

local players = game.Players:GetPlayers()

for i=1, #players do
	local player = players[i]
	if player.Character ~= nil then
		script.ShakeScript:clone().Parent = player.Character
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		script.ShakeScript:clone().Parent = character
	end)
end)

local script

while true do	
	local cam = workspace.CurrentCamera
	cam.CFrame = cam.CFrame*CFrame.Angles(math.random(-3,3)/360,math.random(-3,3)/360,math.random(-3,3)/360)
cam.FieldOfView = math.random(69,71)
	wait()
end

The issue is I will use a region script and insert all of this into it and it just plays as soon as I start the game

2 comments

0

I put your code in a code block via selecting the code and clicking the blue Lua button. It is expected that you do this yourself next time. Also cam is a constant, so why is it in the while loop.

0

I apreicate that, I will do that next time and im a new to scripting mathew420 provided me that last part of the script,I just didnt bother it the camera works, but I cant get this script to run in a specific area.

1 answer

✓ Accepted answer

There are multiple ways to get this to work in a region. In this example, it assumes you are using Region3.

A simple formula to get a Region3 out of a part's size and position is

pos - size/2, pos + size/2

local part = ... -- you define this
local partRegion = Region3.new(part.Position - part.Size/2, part.Position + part.Size/2);

The Workspace has a function which is :FindPartsInRegion3. There are other variants, though searching the developer wiki will tell you those.

:FindPartsInRegion3 returns a table of all the parts in the region provided.

local Workspace, Players = game:GetService("Workspace"), game:GetService("Players");
local parts = Workspace:FindPartsInRegion3(region, nil, math.huge);
-- order: Region3 region, Instance toIgnore (so it wont be included in the table), int maxParts (the maximum parts to be counted

You can traverse the table to get any players that might be in there. A body part can be in the region, and the Players service has a handy :GetPlayerFromCharacter function that lets us get the player from their character.

for _, part in ipairs(parts) do
	local client = Players:GetPlayerFromCharacter(part.Parent); -- client is the player instance now
end

This function can return nil if it does not belong to a player, so check that the player exists!

if (client) then
	-- do something!
end

You might want to check constantly as well, and there is no event that is fired when a part enters a Region3, and BasePart.Touched event isn't very reliable, and most of the body parts of a player have CanCollide set to false, and BasePart:GetTouchingParts() only returns a table of parts that have CanCollide set to true.

You can, however use a while loop to check, waiting a reasonable time, preferably 1 second.

I am leaving all of that to you, as I want you to learn

1 comment

0

Thanks for the help I honestly tried everything seems so simple now haha

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