generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Automatic Gun Script

Previous Thread :: Next Thread 
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
14 May 2014 07:03 PM
Ive tried everything from checking all the variables to printing in every location, but there is no output and the script doesnt work. All the variables are defined and nothing is working, fix please.



local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Handle = Tool.Handle
local Firing = false
local Ammo = Player:WaitForChild("PlayerGui"):WaitForChild("AmmoGui").TrueFrame.RealFrame.AmmoText.Ammo
local MaxAmmo = script.MaxAmmo
local Mags = Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.MagText.Mags
local Debounce = false
local Damage = script.Damage.Value



function unSelected()
Firing = false
Player.PlayerGui.AmmoGui.TrueFrame.Visible = false
end

function Reload()
if Mags.Value > 0 then
for i=1, (MaxAmmo - Ammo.Value) do
Ammo.Value = Ammo.Value + 1
wait(0.1)
end
Mags.Value = Mags.Value - 1
else
if not Debounce then
Debounce = true
for i=1,2 do
Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.MagText.TextColor3 = Color3.new(1,0,0)
wait(0.5)
Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.MagText.TextColor3 = Color3.new(0,1,0)
wait(0.5)
end
Debounce = false
end
end
end

function Fire(Aim)
if Ammo > 0 then
Ammo.Value = Ammo.Value - 1
if mouse.Target then
if (mouse.Hit.p).Parent:FindFirstChild("Humanoid") then
(mouse.Hit.p).Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
end
end
else
Firing = false
Reload()
end
end

function Shooting(mouse)
Firing = true
while wait(0.2) do
Fire(mouse)
if not Firing then
break
end
end
end

function Release(mouse)
Firing = false
end

function onSelected(mouse)
if not Player.PlayerGui:FindFirstChild("AmmoGui") then
s = script.AmmoGui:Clone()
s.Parent = Player.PlayerGui
s.TweenScript.Disabled = false
elseif Player.PlayerGui:FindFirstChild("AmmoGui") then
Player.PlayerGui.AmmoGui.TrueFrame.Visible = true
end
mouse.Button1Up:connect(function()
Release(mouse)
end)
mouse.Button1Down:connect(function()
Shooting(mouse)
end)
end

script.Parent.Equipped:connect(onSelected)
script.Parent.Unequipped:connect(unSelected)
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
14 May 2014 07:06 PM
Ill check the feedback tomorrow, help if you can!
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:25 PM
Bump
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:33 PM
Bump
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:39 PM
Bump
Report Abuse
youssef04 is not online. youssef04
Joined: 22 Jan 2011
Total Posts: 1745
15 May 2014 03:40 PM
What DOESN'T this do?
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:42 PM
It just doesnt work all together. When I select the gun, no gui is cloned into my PlayerGui meaning that the onSelected function didnt run, without it, nothing else would run(except unSelected, but there's no use for that if I cant fire anyways).
Report Abuse
youssef04 is not online. youssef04
Joined: 22 Jan 2011
Total Posts: 1745
15 May 2014 03:43 PM
Isn't it Equipped, not Selected?
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:44 PM
Im naming function names, not events. My events are fine, else it would've errored and something would actually be in the output.
Report Abuse
youssef04 is not online. youssef04
Joined: 22 Jan 2011
Total Posts: 1745
15 May 2014 03:47 PM
Try this on this line:
if Player.PlayerGui:FindFirstChild("AmmoGui") ~= nil then
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:50 PM
Doesnt work. I even used print("hi") right when the function should start at, but nothing shows up.
Report Abuse
powertool is not online. powertool
Joined: 01 Feb 2008
Total Posts: 3771
15 May 2014 03:53 PM
Try this. It sounds like something with variables and things assuming other things when those other things aren't what the things assume them to be.

local Tool
local Player
local Handle
local Firing = false
local Ammo
local MaxAmmo
local Mags
local Debounce = false
local Damage = script.Damage.Value

function unSelected() --What I did was change the way your variables are defined. I ensured that they first get set when you select the tool, instead of assuming they're there. I'm nilling them here to prevent dual definition.
Firing = false
Player.PlayerGui.AmmoGui.TrueFrame.Visible = false
Tool = nil
Player = nil
Handle = nil
Ammo = nil
MaxAmmo = nil
Mags = nil
end

function Reload()
if Mags.Value > 0 then
for i=1, (MaxAmmo - Ammo.Value) do
Ammo.Value = Ammo.Value + 1
wait(0.1)
end
Mags.Value = Mags.Value - 1
else
if not Debounce then
Debounce = true
for i=1,2 do
Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.MagText.TextColor3 = Color3.new(1,0,0)
wait(0.5)
Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.MagText.TextColor3 = Color3.new(0,1,0)
wait(0.5)
end
Debounce = false
end
end
end

function Fire(Aim)
if Ammo > 0 then
Ammo.Value = Ammo.Value - 1
if mouse.Target then
if (mouse.Hit.p).Parent:FindFirstChild("Humanoid") then
(mouse.Hit.p).Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
end
end
else
Firing = false
Reload()
end
end

function Shooting(mouse)
Firing = true
while wait(0.2) do
Fire(mouse)
if not Firing then
break
end
end
end

function Release(mouse)
Firing = false
end

function onSelected(mouse)
if not Player.PlayerGui:FindFirstChild("AmmoGui") then
s = script.AmmoGui:Clone()
s.Parent = Player.PlayerGui
s.TweenScript.Disabled = false
elseif Player.PlayerGui:FindFirstChild("AmmoGui") then
Player.PlayerGui.AmmoGui.TrueFrame.Visible = true
end
mouse.Button1Up:connect(function()
Release(mouse)
end)
mouse.Button1Down:connect(function()
Shooting(mouse)
end)
end


function onEquipped() --I redefined the six crucial variables from the top here.
Tool = script.Parent
Player = game.Players.LocalPlayer
Handle = Tool.Handle
Ammo = Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.AmmoText.Ammo
MaxAmmo = script.MaxAmmo
Mags = Player.PlayerGui.AmmoGui.TrueFrame.RealFrame.MagText.Mags

script.Parent.Equipped:connect(onSelected)
end

script.Parent.Equipped:connect(function() onEquipped() end) --I like calling it the long way. Makes it easier to add a few things.
script.Parent.Unequipped:connect(unSelected)
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 03:53 PM
OHHH I found the problem, its this variable.

local Ammo = Player:WaitForChild("PlayerGui"):WaitForChild("AmmoGui").TrueFrame.RealFrame.AmmoText.Ammo

It waits until AmmoGui shows up but that doesnt happen until you equip the tool, which will never happen since it's waiting..
Report Abuse
powertool is not online. powertool
Joined: 01 Feb 2008
Total Posts: 3771
15 May 2014 03:57 PM
Check the one I did. Another thing you assume in your coding is that the tool is where it's supposed to be. I went through and explicitly defined those variables that need it, when the tool is selected. Those definitions are broken when your tool is deselected.

And yeah, I changed my assumption for Ammo that the GUI would be there. Granted, defined after the gun was selected.
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
15 May 2014 04:00 PM
I got it to work, I didnt use your script completely power, but I used some of your concepts which helped a lot. Thanks for helping!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image