alright so i got a gun, its automatic but idk how to add a reloading system to it. there are 2 scripts. the first one is the local script:
local gun = script.Parent
local gun_shot = gun["Gun Shot"]
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local shooting = false
local equipped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseConnection
gun.Equipped:Connect(function()
equipped = true
mouseConnection = mouse.Button1Down:Connect(function()
shooting = true
while shooting and equipped do
remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
gun_shot:Play()
mouse.Button1Up:Connect(function()
shooting = false
end)
wait(0.1)
end
end)
end)
gun.Unequipped:Connect(function()
equipped = false
mouseConnection:Disconnect()
end)
now here’s the serverscriptservice:
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local damage_amount = 10
remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
local bullet = Instance.new('Part')
bullet.Name = 'Bullet'
bullet.Parent = game.Workspace
bullet.Shape = Enum.PartType.Cylinder
bullet.Size = Vector3.new(0.25, 0.25, 0.25)
bullet.BrickColor = BrickColor.new('White')
local speed = 250
bullet.CFrame = CFrame.new(gunPos, mosPos)
bullet.Velocity = bullet.CFrame.lookVector * speed
bullet.Touched:Connect(function(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid and humanoid.Parent.Name ~= player.Name then
if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 then
end
humanoid:TakeDamage(damage_amount)
bullet:Destroy()
end
end)
game:GetService('Debris'):AddItem(bullet, 1)
end)
1 Like
like what strangefancypants has stated, have two values:
one for max bullets (inside script)
local Max_shots = 6 --change to how much the gun can have
and another for the bullets themselves (inside model)
local remain_shots = script.Parent.NumberValue --put Max_shots here as maximum
when bullets are fired, minus the remain_shots by 1 and keep doing it until it reaches zero
when the player triggers the gun again, run an if check to see if remain_shots is equal/not equal to zero to warn the player to reload.
then when a certain action is triggered (gun trigger or key press), run an if check again to compare the Max_shots and remain_shots. If they are equal, do nothing. Else:
local needed_shots = Max_shots - remain_shots
remain_shots.Value += needed_shots
add visual effects if needed.
1 Like
hi, im not trying to be rude or something but i already found a better and shorter script. but still thank you all for trying to help me!
1 Like
Can you please share the Script? It would help Developers who are facing the same issue find a solution easily.
1 Like
local gun = script.Parent
local empty = gun.Handle.clip_empty
local gun_sound = script.Parent.Handle.GunShot
local reload = gun.Handle.Reload
local userinput = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent2')
gun.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if gun.Ammo.Value > 0 then
remoteEvent:FireServer(gun.Handle.Position, mouse.Hit.p)
gun_sound:Play()
gun.Ammo.Value = gun.Ammo.Value - 1
script.Enabled = false
wait(0.3)
script.Enabled = true
else
empty:Play()
end
end)
end)
userinput.InputBegan:Connect(function(intput, gameProcessed)
if not gameProcessed then
if intput.UserInputType == Enum.UserInputType.Keyboard then
local keycode = intput.KeyCode
if keycode == Enum.KeyCode.R then
reload:Play()
reload.Ended:Wait()
gun.Ammo.Value = 10
end
end
end
end)
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local damage_amount = 10
remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
local bullet = Instance.new('Part')
bullet.Name = 'Bullet'
bullet.Parent = game.Workspace
bullet.Shape = Enum.PartType.Cylinder
bullet.Size = Vector3.new(0.25, 0.25, 0.25)
bullet.BrickColor = BrickColor.new('White')
local speed = 250
bullet.CFrame = CFrame.new(gunPos, mosPos)
bullet.Velocity = bullet.CFrame.lookVector * speed
bullet.Touched:Connect(function(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid and humanoid.Parent.Name ~= player.Name then
if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 then
end
humanoid:TakeDamage(damage_amount)
bullet:Destroy()
end
end)
game:GetService('Debris'):AddItem(bullet, 1)
end)
I’d also reccommend using ContextActionService for context actions like reloading. do NOT use userinputservice for something like this, activate a context when the tool is equipped. Example:
local function Reload()
-- DO NOT DO THIS ON THE CLIENT, THIS IS AN EXAMPLE, YOU'LL MORE THAN LIKELY BE FIRING A REMOTEEVENT INSTEAD
for i = 1, maxbullets - bullets do
if bullets == maxbullets or not canreload then return end
bullets += 1 --[[if you have loop reloading]]
task.wait(reloadtime)
-- etc
end
end)
tool.Equipped:Connect(function()
ContextActionService:BindAction("Reload", Reload--[[This is a function, you'll have to make a function for this]], true --[[Should it create mobile buttons?]], Enum.KeyCode.R --[[Keys, you can add multiple seperated by a "," comma]])
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction("Reload")
end)
tool.Activated:Connect(function()
if bullets <= 0 then return end
end)
1 Like