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.
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”.
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)
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)