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()
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.
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.
Looking at the code itself as well, I don’t see anything that would cause a crash.
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.
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.