What's wrong with this client script that's making my studio crash?

  1. What do you want to achieve? This script keeps causing me to crash.

  2. What is the issue? Every time I playtest this script, my studio crashes.

  3. What solutions have you tried so far? I’ve tried googling.

Client script:
image_2021-05-30_152541

Add a wait() into the loop then it shouldn’t crash

1 Like

Ok, so a few things

  1. You are using a While loop with no wait. If you want to use a while loop add wait() function
  2. You are using event Mouse.Button1Down. This is an event, not a variable, so cannot be used as a conditional in a while loop. you can just put all the code in the function part of the event

As of right now, you can just delete the while loop parts and keep the mouse up and down events. Remeber that they are events, so they will check forever (unless you disconnect) and run the function, so no need for loops

So somethings to keep in mind:
Events are NOT variables
Events should not be used in loops, as they themselves loop forever and check if the condition is met

But what are events anyway?
Events are sorta like functions. They are not magic stuff or anything. They are like functions where you put in a another function to run when an event occurs. That is why you don’t want to always call the function because you dont need to

That probably is a bad way of scripting a fire event.

You should simply just get rid of the while true do and do something like

mouse.Button1Down:Connect(function()
fire=true
while fire do
-- fire the gun
wait()
end
end)
mouse.Button1Up:Connect(function()
fire=false
end)
2 Likes

Strange, doesn’t seem to fire at all

Just do

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Holding = false

Mouse.Button1Down:Connect(function()
    Holding = true

    while Holding do
        wait()
    end
end)

Mouse.Button1Up:Connect(function()
    Holding = false
end)

Also I’d recommend using UserInputService for detecting Mouse Input

Unfortunately, it doesn’t seem to fire at all

Are you sure…? Can you implement print statements or try this instead?

local Player = game.Players.LocalPlayer
local Holding = false
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, Chatted)
    if Chatted then
        return
    end

    print("This should at least work")
    if Input.UserInputType == Enum.UserInputType.MouseButton1 then
        Holding = true
    end
end)

UIS.InputEnded:Connect(function(Input, Chatted)
    if Chatted then
        return
    end

    print("This should at least work")
    if Input.UserInputType == Enum.UserInputType.MouseButton1 then
        Holding = false
    end
end)

while true do
    if Holding == true then
        --Do stuff here
    end
    wait()
end

If I’m supposed to just copy and paste, it doesn’t work

Am I supposed to add something with it?

Are you putting the LocalScript inside StarterPlayerScripts…?

I have it in the starter pack for tools

Are you at least checking your Output for anything that’s outputted back?

Is this what it’s supposed to look like:

image_2021-05-30_170115

Yeah that’s a lot of prints LOL

Keep in mind that we’ve just provided the script so that it doesn’t crash, you have to implement the gun shooting yourself

1 Like