Simple gun script remote events help

I’ve been working on this simple gun script to try to improve my scripting skills. At the moment it works, but only for the user, since it runs on a local script. I’ve been trying to connect it to a remote event or function, but so far that hasn’t really worked. Could someone maybe give me some tips as to how I could go about doing that? I’ve looked over the wiki and various tutorials and tried to follow them as closely as possible, but am still having problems.

This is the original local script.

local repStor = game:GetService("ReplicatedStorage")
local Bullet = repStor.Bullet

local Tool = script.Parent
local Handle = script.Parent.Handle


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

local newBullet = Bullet:Clone()	

Tool.Activated:Connect(function()
	newBullet.Position = Handle.Position
	newBullet.Parent = game.Workspace
	newBullet.Velocity = mouse.Hit.LookVector * 1000
end)

newBullet.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent.Parent then
		hit.Parent.Humanoid.Health = 0
	end
end)

This is the local script after I inserted remote events:

local repStor = game:GetService("ReplicatedStorage")
local Bullet = repStor.Bullet
local FireGun = repStor.FireGun
local OnKillPlayer = repStor.OnKillPlayer

local Tool = script.Parent
local Handle = script.Parent.Handle
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

local newBullet = Bullet:Clone()	

Tool.Activated:Connect(function()
	FireGun:FireServer(mouse)
end)

newBullet.Touched:Connect(function(hit)
	OnKillPlayer:FireServer()
end)

This is the unfinished script in ServerScriptService:

local repStor = game:GetService("ReplicatedStorage")
local FireGun = repStor.FireGun
local OnKillPlayer = repStor.OnKillPlayer
local Bullet = repStor.Bullet

local Tool = game.StarterPack.Tool
local Handle = Tool.Handle


local Players = game:GetService("Players")
local player = Players.LocalPlayer


local newBullet = Bullet:Clone()	

local function onFireGun(player,  mouse)
newBullet.Position = Handle.Position
newBullet.Parent = game.Workspace
newBullet.Velocity = mouse.Hit.LookVector * 1000
end

local function onKillPlayer(player, hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent.Parent then
		hit.Parent.Humanoid.Health = 0
	end
end

FireGun.OnServerEvent:Connect("onFireGun")

OnKillPlayer.OnServerEvent:Connect("onKillPlayer")

I think one of the main problems is the fact that I don’t know which values I need to pass from the local script and vice versa. This problem includes the fact that I don’t understand how to use the mouse when it can only be accessed from the local script. Any help would be much appreciated.

1 Like

Do you have any errors in the output?

Yes, ‘Attempt to connect failed: Passed value is not a function’, which I don’t really understand. It refers to the functions “OnFireGun” and “OnKillPlayer”.

Does it specify which line on the script?

This is my output:
13:47:58.187 - Baseplate auto-recovery file was created

13:48:08.859 - Attempt to connect failed: Passed value is not a function

13:48:08.860 - Stack Begin

13:48:08.860 - Script ‘ServerScriptService.Script’, Line 28

13:48:08.861 - Stack End

13:48:08.861 - Attempt to connect failed: Passed value is not a function

13:48:08.861 - Stack Begin

13:48:08.862 - Script ‘ServerScriptService.Script’, Line 30

13:48:08.862 - Stack End

13:57:08.025 - Baseplate auto-recovery file was created

still getting issues within your code recheck it if cant then i can assist you with that

pass the player argument to the server

local repStor = game:GetService("ReplicatedStorage")
local Bullet = repStor.Bullet
local FireGun = repStor.FireGun
local OnKillPlayer = repStor.OnKillPlayer

local Tool = script.Parent
local Handle = script.Parent.Handle
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

local newBullet = Bullet:Clone()	

Tool.Activated:Connect(function()
	FireGun:FireServer(player, mouse)
end)

newBullet.Touched:Connect(function(hit)
	OnKillPlayer:FireServer(player)
end)

then the server code

local repStor = game:GetService("ReplicatedStorage")
local FireGun = repStor.FireGun
local OnKillPlayer = repStor.OnKillPlayer
local Bullet = repStor.Bullet

local Tool = game.StarterPack.Tool
local Handle = Tool.Handle


-- no players here because it is already an argument

local newBullet = Bullet:Clone()	

local function onFireGun(player,  mouse)
newBullet.Position = Handle.Position
newBullet.Parent = game.Workspace
newBullet.Velocity = mouse.Hit.LookVector * 1000
end

local function onKillPlayer(player, hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent.Parent then
		hit.Parent.Humanoid.Health = 0
	end
end

FireGun.OnServerEvent:Connect(onFireGun)

OnKillPlayer.OnServerEvent:Connect(onKillPlayer)

Ah thanks. That clears up a bit.

A error is being called saying “Hit is not a valid member of Player” on line 17 of the server script.

Try just doing this:

FireGun:FireServer(mouse)

RemoteEvents add the player parameter automatically, so when you add the parameter yourself, you are sending the player twice.

The same thing for OnKillPlayer, you should not pass any parameters because the player parameter is automatic.

The player error has gone now. However, the error “ServerScriptService.Script:17: attempt to index nil with ‘Hit’” has now appeared for line 17 of the server script. I think this arises from the fact that ‘mouse’ has not been properly defined in the server script. I don’t know how to do this given the fact that GetMouse can only be used in local scripts.

I’ve fixed the issue, which was that I was passing ‘mouse’ from the local script to the server script instead of mouse.Hit. I then also passed the tool to the server script from the local script. I condensed the script, too, putting one of the functions inside of the other and so running the whole script off of a single remote event.

Although the line stopping the player from killing itself doesn’t work.

Local script with edits:

local repStor = game:GetService("ReplicatedStorage")
local Bullet = repStor.Bullet
local FireGun = repStor.FireGun

local Tool = script.Parent
local Handle = script.Parent.Handle
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

Tool.Activated:Connect(function()
local firePosition = mouse.Hit
	FireGun:FireServer(firePosition, Handle)
end)

Server script with edits:

local repStor = game:GetService("ReplicatedStorage")
local FireGun = repStor.FireGun
local Bullet = repStor.Bullet	

local function onFireGun(player,  firePosition, Handle)
local newBullet = Bullet:Clone()
newBullet.Position = Handle.Position
newBullet.Parent = game.Workspace
newBullet.Velocity = firePosition.LookVector * 1000

newBullet.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")  and hit.Parent ~= Handle.Parent.Parent.Parent then
		hit.Parent.Humanoid.Health = 0
	end
end)
end

FireGun.OnServerEvent:Connect(onFireGun)