hey i have ran into a problem with my gun script, so basically im trying to have ammo to this gun and i did a mistake while testing the gun only when the tool is in the StarterPack
so now the UI wont show or work,
when its parent is set to workspace or anywhere else than the starterpack
since the players wont spawn with the tool, i need something to replace the “gui” variable so
the ui shows up whatever the tools parent is before its in a players backpack and startergear
heres the code
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Tool:Tool = script.Parent
local Handle = Tool:FindFirstChild("Handle")
local ShootPart = script.Parent.Handle:FindFirstChild("Attachment")
local RemoteEvent = Tool:FindFirstChild("RemoteEvent")
local gui = script.Parent.Parent.Parent.PlayerGui:WaitForChild("AmmoGui") --this needs to be changed
local ammocount = gui:WaitForChild("AmmoCount")
local guiname = gui:WaitForChild("gunname")
local auto = gui:WaitForChild("auto")
local OnCooldown = false
local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=13826956832"
local track
local Do = {
Damage = 15;
Cooldown = 0.3;
Visualize = true;
ShootSound = "rbxassetid://5096003821";
}
local ammo = 25
local maxammo = ammo
local reloading = false
local function reload()
if reloading then return end
reloading = true
tool.Handle.reload:Play()
tool.flash.Enabled = false
tool.Handle.PointLight.Enabled = false
wait(2.5)
tool.flash.Enabled = true
ammo = maxammo
reloading = false
end
RemoteEvent.OnServerEvent:Connect(function(Player,Received)
if not OnCooldown and not reloading then
OnCooldown = true
task.delay(Do.Cooldown,function()
OnCooldown = false
end)
if ammo < 1 then
tool.Handle.noammo:Play()
tool.flash.Enabled = false
tool.Handle.PointLight.Enabled = false
reload()
wait(4)
end
local Origin = ShootPart.WorldPosition
local Direction = (Received.Position-Origin).Unit*3000
local Raycast = workspace:Raycast(Origin,Direction)
local Intersection = Raycast and Raycast.Position or Origin + Direction
local Distance = (Origin - Intersection).Magnitude
local Visualizer = Instance.new("Part")
Visualizer.CanTouch = false
Visualizer.CanCollide = false
Visualizer.CanQuery = false
Visualizer.CastShadow = false
Visualizer.Anchored = true
Visualizer.Material = Enum.Material.Metal
Visualizer.Color = Color3.fromRGB(255, 255, 127)
Visualizer.Size = Vector3.new(.15,.15,Distance)
Visualizer.CFrame = CFrame.new(Origin, Intersection)*CFrame.new(0,0,-Distance/2)
if Do.Visualize == true then
Visualizer.Parent = workspace
TweenService:Create(Visualizer,TweenInfo.new(.3),{Transparency = 1, Size = Vector3.new(0,0,Distance)}):Play()
Debris:AddItem(Visualizer,.35)
ammo = ammo -1
elseif ammo < 1 then
print("reload")
tool.Handle.noammo:Play()
tool.flash.Enabled = false
tool.Handle.PointLight.Enabled = false
reload()
end
if (Do.ShootSound ~= "" or Do.ShootSound ~= nil) then
local Sound = Instance.new("Sound")
Sound.SoundId = Do.ShootSound
Sound.Volume = .35
Sound.PlayOnRemove = true
Sound.Parent = Handle
Sound:Destroy()
end
script.Parent.Equipped:Connect(function()
gui.Enabled = true
guiname.Text = tool.FullName.Value
track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action
track.Looped = true
track:Play()
ammocount.Text = ammo.. "/" ..maxammo
if tool.Automatic.Value == true then
auto.Text = "Automatic"
elseif tool.Automatic.Value == false then
auto.Text = "Not Automatic"
end
end)
script.Parent.Unequipped:Connect(function()
guiname.Text = " "
track:Stop()
gui.Enabled = false
end)
local function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
local function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
if Raycast then
local Hit:Part = Raycast.Instance
local Zombie = (Hit.Parent:FindFirstChild("Zombie") or Hit.Parent.Parent:FindFirstChild("Zombie"))
if Zombie then
UntagHumanoid(Zombie)
TagHumanoid(Zombie, Player)
Zombie.Health = Zombie.Health -Do.Damage
end
end
end
end)
script.Parent.Equipped:Connect(function()
gui.Enabled = true
guiname.Text = tool.FullName.Value
track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action
track.Looped = true
track:Play()
ammocount.Text = ammo.. "/" ..maxammo
if tool.Automatic.Value == true then
auto.Text = "Automatic"
elseif tool.Automatic.Value == false then
auto.Text = "Not Automatic"
end
end)
script.Parent.Unequipped:Connect(function()
guiname.Text = " "
track:Stop()
gui.Enabled = false
end)
this is pretty hard for me to explain so feel free to ask if you dont understand something