How do I make my gun server sided?

#Disclaimer, I can not script very well and this is my first project, please when replying use simple words!#

Hello!
Im making my first project (A FPS Game) and I realized that the gun script I was using was client sided, this script is simple so I don’t think it would be hart to make it server sided, but because I have little to know scripting experience I need the help of the devforum.

The script i’m using:

local plr = game.Players.LocalPlayer
local ms = plr:GetMouse()
local useable = false

function shot()
if useable == true then
script.Parent.CoolDown2.Disabled = true
local bullet = Instance.new(“Part”)
bullet.Shape = “Ball”
bullet.Size = Vector3.new(0.4,0.4,0.4)
bullet.Parent = game.Workspace
bullet.CFrame = ms.Hit
bullet.Anchored = false
bullet.Transparency = 1
bullet.CanCollide = false
function hits(s)
if s.Parent:FindFirstChild(“Humanoid”) then
s.Parent.Humanoid.Health = s.Parent.Humanoid.Health - 70
bullet:remove()
script.Parent.CoolDown2.Disabled = false
end
end
bullet.Touched:connect(hits)
end
end
ms.Button1Down:connect(shot)

script.Parent.Equipped:connect(function()
useable = true
end)
script.Parent.Unequipped:connect(function()
useable = false
end)

1 Like

Use a remote event so it can be server sided.

Remote event;
Insert a remote event instance into replicated storage.

-- Client
local plr = game.Players.LocalPlayer
local ms = plr:GetMouse()
local useable = false

local tool = script.Parent
-- Index the event
local event = game.ReplicatedStorage.RemoteEvent

tool.Activated:Connect(function()
	if useable then
		-- Transfer the mouse CFrame to the server
		event:FireServer(ms.CFrame.Position)
	end
end)

tool.Equipped:Connect(function()
	useable = true
end)

tool.Unequipped:Connect(function()
	useable = false
end)
-- Server
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(mousePos)
-- Rest of your script here
end)
1 Like

The problem is I wouldn’t know how to add it into the script.

1 Like

However do install some security checks on the server to prevent exploitation.

1 Like

The problem is that because I have little to no scripting experience like I do not know what to put were in the script. (I can figure out the rest its just that i’m a beginner.)

1 Like

Fill the vacant spaces in the server script that I marked below “rest of your script here” with your original function, where only the variable mousePos would be the vector form of Mouse.Hit.

tool.Activated:Connect(function()
	if useable then
		-- Transfer the mouse CFrame to the server and the tool
		event:FireServer(ms.Hit, tool)
	end
end)
-- Server
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(msCFrame, tool)
    tool.Cooldown2.Disabled = true
    local bullet = Instance.new("Part")
    -- same lines until
    bullet.CFrame = msCFrame
    bullet.Touched:Connect(function(hit)
        -- hit detection function
        if hit.Parent:FindFirstChild("Humanoid") then
          -- same lines from your function until
          tool.CoolDown2..Disabled = false
       end
    end)
    
end)

Copy paste portions of your server script into the part I marked starting from the beginning lines, replace script.Parent with tool and mouse.CFrame with msCFrame.