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

Gun script works locally, but doesn't work on a server test?

I have a local script that detects when a tool is activated. When the tool is activated, it fires a remote event that fires a bullet from the gun. It works fine when you play test, but when I test it on a server, it doesn't work. What am I doing wrong?

Edit: I have the print scripts to test what isn't working. The only thing that it printed was "oy mate im activated."

(Filtering is enabled)

Local Script:

local repStorage = game:GetService("ReplicatedStorage") local remote = repStorage:WaitForChild("Remote")

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")


_G.Gun = script.Parent


_G.Gun.Activated:connect(function()
	remote:FireServer()
	print("oy mate im activated")
end)

Script:

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")


remote.OnServerEvent:connect(function()
		
			local Player = game.Players.LocalPlayer
			
			
			local Ammo = 5
			local mouse = Player:GetMouse()
		
		
			if Ammo > 0 then
				print("Hey I shot a thing!!")
				local Bullet = Instance.new("Part", workspace)
				game.Debris:AddItem(Bullet, 2)
				Bullet.Shape = "Ball"
				Bullet.Size = Vector3.new(3, 3, 3)
				Bullet.TopSurface = "Smooth"
				Bullet.BottomSurface = "Smooth"
				Bullet.BrickColor = BrickColor.new("Dark stone grey")
				Bullet.CanCollide = false
				Bullet.CFrame = _G.Gun.Handle.CFrame
				Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p)
				local v = Instance.new("BodyVelocity", Bullet)
				v.velocity = Bullet.CFrame.lookVector *90
				v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
				
			end
		
end)

Thanks!

1 answer

✓ Accepted answer

You can't access the player's mouse through a server script. Instead, use a RemoteEvent to pass what you need.

remote:FireServer(mouse.Hit.p)

This is because the client doesn't send their mouse to the server. It doesn't replicate.

You may think to yourself "why don't I just do this?"

--DON'T USE THIS
remote:FireServer(mouse)

But again, you're in the same dilemma. The server will only receive nil.

In conclusion, send the mouse.Hit.p. This is a Vector3, which the server WILL recognize!

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