Game crashes when I pull out a gun I made

Hi, I was trying to make my first gun for future games and also just since I am starting to script. I was making my gun off of a dev forum post by incapz and also alvinblox’s newest video. I did it and all but when I pull out my gun my studio crashes and I have to restart my pc completely. I would add the script and all but my pc is currently restarting. I don’t know what’s going on but thanks in advance.

Frontend script
local tool = script.Parent

local remote = tool:WaitForChild(“OnShoot”)

players = game:GetService(“Players”)

client = players.LocalPlayer

cursor = client:GetMouse()

tool.Activated:Connect(function()

remote:FireServer(cursor.Hit.Position)

end)

Backend script

local tool = script.Parent
local shoot_part = tool:WaitForChild(“Shoot”)
local remote = tool:WaitForChild(“OnShoot”)

local Workspace = game:GetService(“Workspace”)
local ServerStorage = game:GetService(“ServerStorage”)

remote.OnServerEvent:Connect(function(player, position)
local origin = shoot_part.Position
local direction = (position - origin).unit
local result = Workspace:Raycast(shoot_part.Position, direction*250)

local intersection = result and result.Position or origin + direction*250
local distance = (origin - intersection).magnitude

local bullet_clone = ServerStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace

if result then
	local part = result.Instance
	local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
	
	if humanoid then
		humanoid:TakeDamage(20)
		
	end
end
wait(0.15)
bullet_clone:Destroy()

end)

Post your code for better support - we aren’t mind readers nor are we working on your project.

Chances are if your Roblox Studio + PC is having issues when the gun is being pulled out, it may be related to a while wait() do loop or something similar running way too many times within a short interval.

Yes, if you have a while loop running without a wait() in it your computer will freeze up because it’s trying to do that loop over and over without pausing to do anything else.

Ok I will my pc is almost rebooted

I edited it now with the scripts

1 Like

Strange…I’m not getting any issues when running your code. This is what my Explorer looks like (what I assume yours looks like too). I changed some names up, but nothing beyond that.
Explorer

Looking at the code itself as well, I don’t see anything that would cause a crash.

Mine is slightly different here is mine:

Is it still crashing now? Not seeing any reason for Roblox Studio to crash in the script, maybe it was a one-time thing?

I can try again but it did it twice

Yes, I just tried it again and it crashed I have no clue what is going on.

And the wierdest thing about it is that I have to restart my pc completely

Is there anything else in the game? If possible, it would make it easier for me to test whether it’s an issue on your part or with a script if you save the place as a file, and upload it here.

The only other thing in the game is the baseplate and a drooling zombie I was going to use as a dummy

If you don’t have anything important, please send the file over so I can use your exact scripts and everything to test (once your PC is restarted).

Ok but be warned if it crashes you have to restart your whole pc and once you get it I will delete it

Sure, feel free to send it over whenever you’re ready.

post was deleted for reasons because I wanted to

Alrighty, I downloaded it. You can delete the post if you want by clicking the 3 dots next to your post, and clicking the trash can.

Alrighty, looking through your place file, I’m 90% sure that the reason why it crashed your Roblox Studio (and mine as well) is due to the high part count. I don’t think Roblox likes 3000+ parts being spawned at the player at the same time. Try cutting down the number of parts on the tool.

Ok I will try to cut down a lot and see what it will be like