You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I am attempting to create a basic gun that shoots and when the ammo is 0 then you can reload it.
- What is the issue? Include screenshots / videos if possible!
Issue 1
When I click R to reload, it does it once and never again. Also after I “reload” I can no longer shoot
Issue 2
When I check the value through the server and the client the number value is
Always 0
I'm not exactly sure what's going wrong with my code but it's to the point where maybe I'm thinking of re-writing it to make it more readable.My code:
Local script (front end)
local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
local GUI = script.Parent.GunInterface
local PlayerGui = client.PlayerGui
local remoteI = tool:WaitForChild("Onreload")
GUI.Parent = PlayerGui
tool.Activated:Connect(function()
remote:FireServer(cursor.Hit.Position)
end)
local b = "bullets"
local amt = 12
PlayerGui.GunInterface.Frame.TextLabel.Text = amt .. b
tool.Equipped:Connect(function()
PlayerGui.GunInterface.Enabled = true
end)
tool.Unequipped:Connect(function()
PlayerGui.GunInterface.Enabled = false
end)
if tool.Enabled == true then
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
if key == "r" then
print("Reloaded")
remoteI:FireServer()
end
end)
end
Server script (backend)
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local bulletamt = script.Parent.Bulletval.Value
local getbv = script.Parent.Bulletval
bulletamt = 12
local remoteI = tool:WaitForChild("Onreload")
local Workspace = game:GetService("Workspace")
getbv.Changed:Connect(function(Change)
print("Change:", Change)
end)
remote.OnServerEvent:Connect(function(player, position)
if bulletamt >= 1 then
local origin = shoot_part.Position
local direction = (position - origin).Unit
local result = Workspace:Raycast(shoot_part.Position, direction*300)
local intersection = result and result.Position or origin + direction*300
local distance = (origin - intersection).Magnitude
local bullet_clone = game.ServerStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
bullet_clone.Transparency = 0.5
if result then
print(result)
local part = result.Instance
print(part)
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
if part.Name == "LeftFoot" or part.Name == "RightFoot" or part.Name == "LeftHand" or part.Name == "RightHand" then
humanoid:TakeDamage(1)
print("1")
elseif part.Name == "Head" then
humanoid:TakeDamage(50)
print("50")
elseif part.Name == "UpperTorso" or part.Name == "LowerTorso" or part.Name == "HumanoidRootPart" then
humanoid:TakeDamage(20)
print("20")
elseif part.Name == "LeftLowerArm" or part.Name == "RightLowerArm" or part.Name == "LeftUpperArm" or part.Name == "RightUpperArm" or part.Name == "Left Arm" or part.Name == "Right Arm" or part.Name == "Right Leg" or part.Name == "Left Leg" then
humanoid:TakeDamage(10)
else
humanoid:TakeDamage(5)
print("10")
end
end
end
print(bulletamt)
bulletamt = bulletamt - 1
print(bulletamt)
--bullet stuff
local b = " bullets"
local amt = bulletamt
script.Parent.GunInterface.Frame.TextLabel.Text = amt .. b
wait(0.25)
bullet_clone:Destroy()
elseif getbv.Value == 0 then
print("No more bullets")
end
end)
remoteI.OnServerEvent:Connect(function(player, client)
if script.Parent.Bulletval.Value == 0 then
script.Parent.Bulletval.Value = 12
print("Reloaded")
end
end)
Explorer view
If you’ve read this far then thank you