hi, i’ve got this script that makes it so that the closer you get to another specific player the more your screen shakes, and it works. but if the specific player resets character and gets teleported to another point of the map, the screen on the local player keeps on shaking. what can i do?
local RunService = game:GetService("RunService")
local deb = false -- Debounce (Switch)
local maxDistance = 60 -- Distance threshold
workspace.ChildAdded:Connect(function()
wait(1)
local children = workspace:GetChildren()
for i = 1, #children do
if children[i]:FindFirstChild("BAD") then
local him = children[i]
local function Check() -- Returns true if entity is near
if (him.PrimaryPart.Position - game.Players.LocalPlayer.Character.PrimaryPart.Position).Magnitude <= maxDistance then
return true, game.Players.LocalPlayer:DistanceFromCharacter(him.PrimaryPart.Position)
end
return false, nil
end
RunService:BindToRenderStep("ShakeCamera", 201, function(dt) --RenderStepped is a better option for loops
local isNear, distance = Check() -- I decided to return the distance if the entity is near.
-- The distance will be used later in the script.
if isNear then
local x = math.random(-2.5, 2.5) / 100 * (maxDistance / distance)
local y = math.random(-2.5, 2.5) / 100 * (maxDistance / distance)
local z = math.random(-2.5, 2.5) / 100 * (maxDistance / distance)
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(x,y,z)math.random(-1, 1) -- Shakes screen randomly
else
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(0,0,0) -- Resets camera
end
end)
end
end
end)
Additionally you could try to connect these types of player’s dying to a RemoteFunction to fix the issue
Example:
NearbyCharacters = {}
local RunService = game:GetService("RunService")
local deb = false -- Debounce (Switch)
local maxDistance = 60 -- Distance threshold
workspace.ChildAdded:Connect(function()
wait(1)
local children = workspace:GetChildren()
for i = 1, #children do
if children[i]:FindFirstChild("BAD") then
local him = children[i]
local function Check() -- Returns true if entity is near
if (him.PrimaryPart.Position - game.Players.LocalPlayer.Character.PrimaryPart.Position).Magnitude <= maxDistance then
return true, game.Players.LocalPlayer:DistanceFromCharacter(him.PrimaryPart.Position)
end
return false, nil
end
RunService:BindToRenderStep("ShakeCamera", 201, function(dt) --RenderStepped is a better option for loops
local isNear, distance = Check() -- I decided to return the distance if the entity is near.
-- The distance will be used later in the script.
if isNear then
table.insert(NearbyCharacters, him) --Use table.insert() to insert character into nearby character table
local x = math.random(-2.5, 2.5) / 100 * (maxDistance / distance)
local y = math.random(-2.5, 2.5) / 100 * (maxDistance / distance)
local z = math.random(-2.5, 2.5) / 100 * (maxDistance / distance)
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(x,y,z)math.random(-1, 1) -- Shakes screen randomly
else
for i, v in pairs(NearbyCharacters) do
if v == him then
table.remove(NearbyCharacters, i)
end
end
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(0,0,0) -- Resets camera
end
end)
end
end
end)
Event.OnServerInvoke = function(Character)
for i, v in pairs(NearbyCharacters) do
if v == Character then
table.remove(NearbyCharacters, i)
--Disable Camera Shake
end
end
end
--Local Script that's inserted into bad player's characters when round or game is started
script.Parent.Humanoid.Died:Connect(function()
Event:InvokeServer(script.Parent.Name)
end)