Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my tool keep getting destroyed on random?

Asked by 4 years ago
Edited 4 years ago

I have a tool that has a script that constantly sets the tool's CFrame to the camera's CFrame with an offset to give it an FPS game like look. That's it.

But when I equip the tool, after some time on random, the tool gets destroyed. When I don't have the tool equipped, it doesn't get destroyed. I am suspecting that when I equip it, the model gets transferred to the workspace and gets affected by gravity, hence falling to the void, but when I log the Y-axis of all the parts, none of them ever are moving downwards, however, their velocities do grow negatively.

I also tried to anchor the tool which caused them to levitate in mid-air instead of being stuck to the camera, but it still got deleted. I also tried to log it ancestor, but it doesn't change at deletion. I have been trying to debug this for about 6 hours now, and I need assistance. Don't hesitate to ask more questions, but I need this solved :)

Note: I did figure this out though: if you equip and unequip the tool very fast, it stops its deletion mysteriously ¯_(?)_/¯

Here's the code:

-- this is a local script because we don't want others to see the floating hands

local runService = game:GetService("RunService")
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local viewModel = game.ReplicatedStorage:WaitForChild("player"):WaitForChild("viewModel"):Clone()
viewModel.Parent = game.Workspace:WaitForChild("storages"):WaitForChild("local"):WaitForChild("viewModels")

local onDie = function ()
    viewModel:Destroy()
end

humanoid.Died:Connect(onDie)

while true do
    runService.RenderStepped:Wait()
    viewModel.Head.CFrame = camera.CFrame
end
0
Thank you for adding th code. raid6n 2196 — 4y
0
You're welcome ABHI1290 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Your tool is falling to the void server-side but is visually on your hand client-side. Anchor it on the server-side, and the un-anchor it on client-side. That should fix it.

0
OMG, this worked. Thank you so much! ABHI1290 0 — 4y
Ad

Answer this question