Changing a gun script from local to server sided

Basically, i have made a gun script, but it only works locally with the tool, and i don’t know how to make it server-sided. Can anyone help me?

Here’s the script:

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

local PartVelocity = 300
local cooldown = false

local Number = 1

local Folder = Instance.new("Folder")
Folder.Name = "Parts Folder"
Folder.Parent = workspace

script.Parent.Activated:Connect(function()
	if cooldown == false then
		cooldown = true
		local Part = Instance.new("Part")
		Part.Name = "Part" .. Number
		Part.CanCollide = false
		Part.Material = Enum.Material.Neon
		Part.BrickColor = BrickColor.new("Lime green")
		Part.Size = Vector3.new(0.5,0.5,0.5)

		game.ReplicatedStorage.GunValue:Clone().Parent = Part

		Part.CFrame = LocalPlayer.Character.RightHand.CFrame
		Part.Velocity = CFrame.new(LocalPlayer.Character.Head.CFrame.p, LocalPlayerGetMouse.Hit.p).lookVector * PartVelocity

		Number += 1
		Part.Parent = Folder
		script.Parent.Shoot:Play()
		wait(1)
		script.Parent.Reload:Play()
		wait(0.5)
		cooldown = false
	end
end)

The script itself does work but i need it to be server-sided, any help is appreciated! :slightly_smiling_face:

Have you heard about RemoteEvent client/server transportation? I believe it can help you

Basically, just send the Mouse’s Hit Position to the server and handle the bullet & bullet’s Velocity from there (Unless if you wanna handle the hit detection on the server that is)

Yeah, i already know remote events and stuff, i’m just really confused on how i’m gonna do it.

If you could point better what i need to send and what to do next it would really help.

So, what I think you could do is first create your Event, define it, then fire it upon activation with some parameters:

Event:FireServer(PartVelocity, MousePosition, Number)

Then you can handle the majority of your LocalScript on your local side to the server (Creating the Part from the server side, using the Velocity from your passed variable, etc)

(Don’t worry I’m confused as well)

1 Like

After a long time making scripts, i’m still struggling to make it, based on that i made these scripts:

Local

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local LocalPlayerGetMouse = LocalPlayer:GetMouse()
local Mouse = LocalPlayerGetMouse.Hit.p

local PartVelocity = 300
local cooldown = false

local Number = 1

script.Parent.Activated:Connect(function()
	if cooldown == false then
		cooldown = true
		
		game.ReplicatedStorage.Events.arma:FireServer(PartVelocity, Mouse, Number)
	end
end)

Server

game.ReplicatedStorage.Events.arma.OnServerEvent:Connect(function(plr, PartVelocity, Mouse, Number)
	local folder = workspace.Tiros
	
	local Part = Instance.new("Part")
	Part.Name = "Part" .. Number
	Part.CanCollide = false
	Part.Material = Enum.Material.Neon
	Part.BrickColor = BrickColor.new("Lime green")
	Part.Size = Vector3.new(0.5,0.5,0.5)
	
	game.ReplicatedStorage.GunValue:Clone().Parent = Part
	
	Part.CFrame = plr.Character.RightHand.CFrame
	Part.Velocity = CFrame.new(plr.Character.Head.CFrame.p, Mouse).lookVector * PartVelocity
	
	Number += 1
	
	Part.Parent = folder
end)

Try and also add some print statements to it so that you can figure out what worked and what didn’t hopefully :thinking:

It’s partially working. At the moment it’s doing what a lot of guns in roblox do:

https://gyazo.com/32cd2a43e26729a76828100c22889fdc

It’s going towards the center of the map, i have seen a lot free models or just roblox guns themselves do that, do you know how i can fix it?

Edit: According to the print everything in the script is working.

Oh I see the issue

You’re only getting the Mouse’s Position once and that’s it, when instead you should be getting the Position time you fire the Event so

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

local PartVelocity = 300
local cooldown = false

local Number = 1

script.Parent.Activated:Connect(function()
	if cooldown == false then
		cooldown = true
		local Mouse = LocalPlayerGetMouse.Hit.p

		game.ReplicatedStorage.Events.arma:FireServer(PartVelocity, Mouse, Number)
	end
end)

Try this instead