How would i transfer this to a serverscript?

Hello, I added some stuff to an old gun script, and it has some problems.

  • It’s a local script, easily exploitable to change ammo, maxammo.

  • I am not that good with the backend server so I’m having trouble doing it.

  • I’m trying to make it just the server script and completely remove the local script.

  • Local Script:

local MaxAmmo = script.Parent:WaitForChild("MaxAmmo").Value
local Ammo = script.Parent:WaitForChild("Ammo").Value
Ammo = MaxAmmo
local Reloading = false
local Mouse
local Input = game:GetService("UserInputService")
local shooting = false
local mConnect
local reloadTime = 3
local idleAnim = Instance.new("Animation", script.Parent)
idleAnim.AnimationId = "rbxassetid://"..script.Parent:GetAttribute("IdleAnimation")
IdleAnimLoad = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(idleAnim)
local shootAnim = Instance.new("Animation", script.Parent)
shootAnim.AnimationId = "rbxassetid://"..script.Parent:GetAttribute("ShootAnimation")
shootAnimLoad = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(shootAnim)
local reloadAnim = Instance.new("Animation", script.Parent)
reloadAnim.AnimationId = "rbxassetid://"..script.Parent:GetAttribute("ReloadAnim")
reloadAnimLoad = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(reloadAnim)
local CameraShaker = require(game.ReplicatedStorage.CameraShaker)
local camera = workspace.CurrentCamera

local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
	camera.CFrame = camera.CFrame * shakeCf
end)


local function Reload()
	if (Mouse)then
		Reloading = true
		Mouse.Icon = "rbxasset://textures/GunWaitCursor.png"
		reloadAnimLoad:Play()
		wait(reloadTime)
		Ammo = MaxAmmo
		Reloading = false
		Mouse.Icon = "rbxasset://textures/GunCursor.png"
	end
end


script.Parent.Equipped:Connect(function(MouseSource)
	IdleAnimLoad:Play()
	local function shoot()
		if Ammo>0 and not Reloading then
			Ammo=Ammo-1
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				game.ReplicatedStorage.Ak47Shoot:FireServer(Mouse.Target.Parent, 50)
				shootAnimLoad:Play()
				camShake:Start()
				camShake:Shake(CameraShaker.Presets.Bump)
				print(Ammo)
			end
		end
	end
	Mouse = MouseSource
	Mouse.Icon = "rbxasset://textures/GunCursor.png"
	script.Parent:WaitForChild("IsEquipped").Value = true
	local plrsMouse = game.Players.LocalPlayer:GetMouse()
	mConnect = plrsMouse.Button1Down:Connect(function()
		shooting = true
		
		while shooting and script.Parent:WaitForChild("IsEquipped").Value == true do
			shoot()
			plrsMouse.Button1Up:Connect(function()
				shooting = false
			end)
			wait(0.1)
		end
	end)
	Input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and Reloading == false and Ammo~=MaxAmmo and script.Parent.IsEquipped.Value == true then
			Reload()
		end	
	end)
end)

script.Parent.Unequipped:Connect(function(MouseSource)
	script.Parent:WaitForChild("IsEquipped").Value = true
	IdleAnimLoad:Stop()
	mConnect:Disconnect()
end)
  • Serverscript (ik its bad lol)
game.ReplicatedStorage.Ak47Shoot.OnServerEvent:Connect(function(plr, target, damage)
	target.Humanoid:TakeDamage(5)
end)
1 Like

I think i still have to keep the local script mainly for the playersMouse

Try using Remote events or functions. Read the article and you can play with your code and see where it gets you to.

2 Likes

Anything that exploiters could abuse needs a check on a ServerScript. Personally, I’d introduce some Attributes into the mix (It’s even recommended by Roblox to use them on weapons) for your ammo, damage, etc. Make your ServerScript take this data directly from the attributes instead of relying on the RemoteEvent.

Yes, you’re right. The mouse data can only be received from the client. To combat shooting through walls, you can do a Raycast on the server between the players’ characters, and check if anything is in the way.

He already briefly used a remote event, so we can presume that he knows the basics of those. Yet, it’s still a good idea to look on that page. You might discover something that you didn’t know about.

(Reloading would also end up needing to be a remote event, as UserInputService also only works on the client, and changing the ammo attribute won’t lead to an automatic replication to the server)

3 Likes

Isn’t FindPartOnRay deprecated or i can still technically use it

Haven’t done Raycasting in a very long while, so you might want to check the documentation for it.

You might be right about some older things being deprecated… a while back Roblox reworked raycasting.

yes now it works, thanks

  • 30 thin