Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why is mouse not being defined in the serverscript? [ANSWERED]

im making a pretty basic gun using raycasting. the only problem i have so far is the mouse not being defined. it works in test mode, but not in a local server. Localscript:

local gun = script.Parent
local plr = game.Players.LocalPlayer
gun.Activated:connect(function()
	local mouse = plr:GetMouse()
	gun.FireEvent:FireServer(mouse)
end)

Serverscript:

local gun = script.Parent
local bodyParts = {"Leg", "Arm", "Torso", "Head"}
local dmg = {20, 20, 30, 50}
gun.FireEvent.OnServerEvent:connect(function(plr, mouse)
	print("fire")
		print("Mouse pressed!")
		local ray = Ray.new(gun.Handle.CFrame.p, (mouse.Hit.p - gun.Handle.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			for i = 1,4 do
			if string.find(part.Name, bodyParts[i]) then
				print(i)
				if not humanoid then
					humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
				end
				if humanoid then
				humanoid:TakeDamage(dmg[i])
			end
			end
			end
			end
	print(part)
	print(position)
end)

3 comments

0

mouse is to be called localscript only, and on line 4 it's Connect not connect. that is another error

0

what are you talking about? connect works just fine. both Connect and connect work.

0

connect is depreciated but to my knowledge will still work (use Connect), are you putting an event inside of the tool? And the script is replicating with each tool as well? To me that doesn't seem like a good idea  

3 answers

✓ Accepted answer

I had this problem a lot too. Basically, Roblox doesn't like it when you send the entire mouse as an argument. Instead, just send mouse.Hit. Here, I put it into your script.

Local Script:

local gun = script.Parent
local plr = game.Players.LocalPlayer
gun.Activated:connect(function()
    local mouse = plr:GetMouse()
    gun.FireEvent:FireServer(mouse.Hit)
end)

Server Script:

local gun = script.Parent
local bodyParts = {"Leg", "Arm", "Torso", "Head"}
local dmg = {20, 20, 30, 50}
gun.FireEvent.OnServerEvent:connect(function(plr, Hit)
    print("fire")
        print("Mouse pressed!")
        local ray = Ray.new(gun.Handle.CFrame.p, (Hit.p - gun.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")
            for i = 1,4 do
            if string.find(part.Name, bodyParts[i]) then
                print(i)
                if not humanoid then
                    humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
                end
                if humanoid then
                humanoid:TakeDamage(dmg[i])
            end
            end
            end
            end
    print(part)
    print(position)
end)

All I did was simply replace mouse with mouse.Hit It's an easy workaround, and also works with mouse.Target.

"Mouse" is a client replicated object. By sending the Instance itself through a remote, you cannot obtain it, as it is a local instance. You could however send data FROM the Mouse, in order to get the same effect.

Ah yes, I made that mistake too.

You cannot send the mouse over to the server, it does not have access to the client's mouse so it will act as nil (non existant).

This discussion is preserved as it was. New answers, comments, and votes are closed.