I am making a horror game and got to the part where I have to make a flashlight. I have a view model where once the player presses F, the right arm comes up while holding a flashlight that is on. Pressing F again brings the left arm up to pull the right arm down off screen while turning off the flashlight.
I am having a problem where once the view model runs an animation, you can see other player’s arms overlapping yours as well. It is supposed to be only the client players’ arms. It shows up for every players clients.
Can I get help making it so ONLY the player’s arms appear and not other’s arms? (This is a model I found on the toolbox and i am trying to fix the bugs to make it better.)
Here is the model i used from the toolbox:
I should also mention, that whenever a new player joins the server the animation on line 76 plays for all clients again, if there is a way to stop this please let me know.
Here is the Explorer of the necessary stuff:
SpringModule:
-- Constants
local ITERATIONS = 8
-- Module
local SPRING = {}
-- Functions
function SPRING.new(self, mass, force, damping, speed)
local spring = {
Target = Vector3.new();
Position = Vector3.new();
Velocity = Vector3.new();
Mass = mass or 5;
Force = force or 50;
Damping = damping or 4;
Speed = speed or 4;
}
function spring.getstats(self)
return self.Mass, self.Force, self.Damping, self.Speed
end
function spring.changestats(self, mass, force, damping, speed)
self.Mass = mass or self.Mass
self.Force = force or self.Force
self.Damping = damping or self.Damping
self.Speed = speed or self.Speed
end
function spring.shove(self, force)
local x, y, z = force.X, force.Y, force.Z
if x ~= x or x == math.huge or x == -math.huge then
x = 0
end
if y ~= y or y == math.huge or y == -math.huge then
y = 0
end
if z ~= z or z == math.huge or z == -math.huge then
z = 0
end
self.Velocity = self.Velocity + Vector3.new(x, y, z)
end
function spring.update(self, dt)
local scaledDeltaTime = dt * self.Speed / ITERATIONS
for i = 1, ITERATIONS do
local iterationForce= self.Target - self.Position
local acceleration = (iterationForce * self.Force) / self.Mass
acceleration = acceleration - self.Velocity * self.Damping
self.Velocity = self.Velocity + acceleration * scaledDeltaTime
self.Position = self.Position + self.Velocity * scaledDeltaTime
end
return self.Position
end
return spring
end
-- Return
return SPRING
FlashlightClient
local userinputservice = game:GetService("UserInputService")
local flashlight = false
local char = script.Parent
local humanoid = char:FindFirstChildWhichIsA("Humanoid")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.Camera
local RunService = game:GetService("RunService")
local flashbutton = Player.PlayerGui.fleshbleight.ImageButton
if userinputservice.TouchEnabled then
flashbutton.Parent.Enabled = true
end
local ViewModel = game.ReplicatedStorage.viewmodel:Clone()
ViewModel.Parent = Camera
local springmodule = require(game.ReplicatedStorage.SpringModule)
local debounce = false
local debounceTime = 1 -- 1 second debounce time
local viewmodelflashlight = ViewModel.Flashlight
local viewmodelidle = ViewModel.Animation:LoadAnimation(game.ReplicatedStorage.Flashlight.ViewmodelUp)
local viewmodeldown = ViewModel.Animation:LoadAnimation(game.ReplicatedStorage.Flashlight.ViewmodelDown)
local viewmodelequip = ViewModel.Animation:LoadAnimation(game.ReplicatedStorage.Flashlight.Viewmodelequip)
local viewmodelunequip = ViewModel.Animation:LoadAnimation(game.ReplicatedStorage.Flashlight.ViewmodelUnequip)
local viewmodelrun = ViewModel.Animation:LoadAnimation(game.ReplicatedStorage.Flashlight.ViewmodelWalk)
local function Bob(addition)
return math.sin(tick() * addition * 1.3) * 0.5
end
local swayspring = springmodule.new()
local bobspring = springmodule.new()
RunService.RenderStepped:Connect(function(dt)
local distance = (Player.Character.Head.Position - workspace.CurrentCamera.CFrame.Position).Magnitude
if distance < 1.5 and ViewModel then
ViewModel.Parent = workspace.CurrentCamera
else
ViewModel.Parent = game.Lighting
end
if Player.Character.Humanoid.Health == 0 then
if ViewModel ~= nil then
ViewModel:Destroy()
end
end
local Delta = game.UserInputService:GetMouseDelta()
swayspring:shove(Vector3.new(-Delta.X/500, Delta.Y/500, 0))
bobspring:shove(Vector3.new(Bob(5), Bob(10), Bob(5)) / 10 * (Character.PrimaryPart.Velocity.Magnitude) / 10)
local UpdatedSway = swayspring:update(dt)
local UpdatedBob = bobspring:update(dt)
if ViewModel ~= nil then
ViewModel:PivotTo(Camera.CFrame * CFrame.new(UpdatedSway.X, UpdatedSway.Y, 0) *
CFrame.new(UpdatedBob.X, UpdatedBob.Y, 0) )
end
--character shirt and color
if char:FindFirstChildWhichIsA("Shirt") then
ViewModel["Left Arm"].Decal.Texture = char:FindFirstChildWhichIsA("Shirt").ShirtTemplate
ViewModel["Right Arm"].Decal.Texture = char:FindFirstChildWhichIsA("Shirt").ShirtTemplate
end
if char:FindFirstChild("Left Arm") then
ViewModel["Left Arm"].Color = char:FindFirstChild("Left Arm").Color
end
if char:FindFirstChild("Right Arm") then
ViewModel["Right Arm"].Color = char:FindFirstChild("Right Arm").Color
end
--shirt and color end
end)
viewmodeldown:Play()
viewmodelflashlight.Light.Light.Enabled = false
viewmodelflashlight.Front.SurfaceLight.Enabled = false
viewmodelflashlight.Light.Shadow.Enabled = false
wait(5) --wait period so player wont press F before animations load in, causing overlapping of anims
userinputservice.InputBegan:Connect(function(input,istyping)--open
if istyping then return end
if input.KeyCode == Enum.KeyCode.F and not debounce then
debounce = true
if flashlight == false then
script.Toggle:FireServer("on")--if no value it means its closing the flashlight
flashlight = true
local lib = char:WaitForChild("Flashlight")
if lib then
lib.Light.Light.Enabled = false
lib.Front.SurfaceLight.Enabled = false
lib.Light.Shadow.Enabled = false
lib.Light.Sound.Volume = 0
lib.Light.Sound2.Volume = 0
end
--viewmodel stuff
viewmodelflashlight.Light.Sound:Play()
viewmodelequip:Play()
viewmodeldown:Stop()
viewmodelidle:Play()
viewmodelflashlight.Light.Light.Enabled = true
viewmodelflashlight.Front.SurfaceLight.Enabled = true
viewmodelflashlight.Light.Shadow.Enabled = true
else
script.Toggle:FireServer()--if no value it means its closing the flashlight
flashlight = false
--viewmodelstuff
viewmodelflashlight.Light.Sound2:Play()
viewmodelunequip:Play()
viewmodeldown:Play()
viewmodelidle:Stop()
viewmodelflashlight.Light.Light.Enabled = false
viewmodelflashlight.Front.SurfaceLight.Enabled = false
viewmodelflashlight.Light.Shadow.Enabled = false
end
task.wait(debounceTime)
debounce = false
end
end)
flashbutton.MouseButton1Click:Connect(function()
if not debounce then
debounce = true
if flashlight == false then
script.Toggle:FireServer("on")--if no value it means its closing the flashlight
flashlight = true
local lib = char:WaitForChild("Flashlight")
if lib then
lib.Light.Light.Enabled = false
lib.Front.SurfaceLight.Enabled = false
lib.Light.Shadow.Enabled = false
lib.Light.Sound.Volume = 0
lib.Light.Sound2.Volume = 0
end
--viewmodel stuff
viewmodelflashlight.Light.Sound:Play()
viewmodelequip:Play()
viewmodeldown:Stop()
viewmodelidle:Play()
viewmodelflashlight.Light.Light.Enabled = true
viewmodelflashlight.Front.SurfaceLight.Enabled = true
viewmodelflashlight.Light.Shadow.Enabled = true
else
script.Toggle:FireServer()--if no value it means its closing the flashlight
flashlight = false
--viewmodelstuff
viewmodelflashlight.Light.Sound2:Play()
viewmodelunequip:Play()
viewmodeldown:Play()
viewmodelidle:Stop()
viewmodelflashlight.Light.Light.Enabled = false
viewmodelflashlight.Front.SurfaceLight.Enabled = false
viewmodelflashlight.Light.Shadow.Enabled = false
end
task.wait(debounceTime)
debounce = false
end
end)
FlashlightServer
local char = script.Parent.Parent
local thirdequip = char:FindFirstChildWhichIsA("Humanoid"):LoadAnimation(game.ReplicatedStorage.Flashlight.EquipThirdPerson)
local thirdidle = char:FindFirstChildWhichIsA("Humanoid"):LoadAnimation(game.ReplicatedStorage.Flashlight.IdleThirdPerson)
local flashlight
local motor
script.Parent.Toggle.OnServerEvent:Connect(function(plr,enabled)
if enabled ~= nil then
flashlight = game.ReplicatedStorage.Flashlight.Flashlight:Clone()
flashlight.Parent = char
motor = Instance.new("Motor6D",char)
motor.Part0 = char:FindFirstChild("Right Arm")
motor.Part1 = flashlight.Handle
thirdequip:Play()
thirdidle:Play()
flashlight.Light.Sound:Play()
else
if flashlight then
flashlight.Light.Sound2:Play()
thirdidle:Stop()
thirdequip:Stop()
if motor then
motor:Destroy()
end
flashlight:Destroy()
end
end
end)
Thanks for any help solving this. I am not the most experienced developer.