Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

AlvinBlox "Howto Make A Gun" tutorial - Gun disappears/unequips a few seconds after equip?

Hi Everyone,

My bro and I have been trying to learn to script roblox. We started first on a bunch of tutorials that were using deprecated code, and using books with deprecated code. After we figured that out - we started searching for newer tutorials.

Enter AlvinBlox - so we start working on his "Make a Gun" tutorial.

We grabbed code directly from his website at: https://www.alvinblox.com/roblox-gun-tutorial-code/ and plugged it into the folders he specifies.

We made our animations. We did the whole thing.

For some reason when we test on our home server with 2 players, it goes like this:

**You enable his gun, it appears in his hand for 5 or 10 seconds. Then it disappears. It also Disappears from inventory.

OK, so I thought "Hey, reset the player, and see what happens.

After resetting the player, I tried readying the gun. It equips, and it does not unequip this time.

**

That's weird.

Any help?, We';d humbly appreciate it.

--Local Script here
local tool = script.Parent -- Getting the tool
local player = game:GetService("Players").LocalPlayer -- Getting the player
local mouse = player:GetMouse() -- Getting the mouse
local sound = tool:WaitForChild("Gunfire")
local torso = "" -- Nothing for now.
local reloading = false -- Variable to check if we are currently reloading
local contextActionService = game:GetService("ContextActionService") -- Allow us to cater for Mobile players
local bodytype = nil -- Nil for now but will check whether player is R6 or R15
local difference = 0 -- Difference between position of head and mouse
local replicatedstorage = game:GetService("ReplicatedStorage")
local gungui = tool:WaitForChild("GunGUI")
local bullets = tool:WaitForChild("Bullets")
local reloadtime = 3
-- Remote Events
local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation")
local headshot = replicatedstorage:WaitForChild("Headshot")
local reload2 = replicatedstorage:WaitForChild("Reload")
local shootevent = replicatedstorage:WaitForChild("ShootEvent")
local unequipanimation = replicatedstorage:WaitForChild("UnequipAnimation")
-- Remote Functions
local checkBodyType = replicatedstorage:WaitForChild("CheckBodyType")
local fetchBulletsLeft = replicatedstorage:WaitForChild("FetchBulletsLeft")
-- Find Body Type 
function findBodyType() -- Used to determine whether a player is R6 or R15
	bodytype = checkBodyType:InvokeServer(tool) -- Invoking the Remotefunction to do a check on the server
	print(bodytype)
end
-- Reloading function
function reload()
	--debounce variable
	reloading = true
	--fire remote event on server, and the argument in () tells which animation to fire as well
	reload2:FireServer(tool.reload)
	--changes mouse icon during reload
	mouse.Icon = "http://www.roblox.com/asset?id=10952419"
	--changes text inside gunGUI to read 'reloading'
	player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Reloading!"
	--sets a wait equals to 3 seconds as defined at top
	wait(reloadtime)
	--restocks bullets by changing global number value
	bullets.Value = 6
	--change text in gunGUI back to read bullet count again
	player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Bullets: "..bullets.Value
	--resets mouse icon
	mouse.Icon = "http://www.roblox.com/asset?id=90980978"
	--fires server event that runs equipanimation
	equipAnimation:FireServer(tool.shoot)
	--sets debounce variable back to false
	reloading = false
end
-- When the tool is equipped, the following event will run
tool.Equipped:Connect(function(mouse)
	gungui:Clone().Parent = player.PlayerGui -- We are cloning the Gun GUI into the player's PlayerGUI
	findBodyType() -- Calling the function above to check the body type.
	equipAnimation:FireServer(tool.shoot) -- Calling the equip animation remoteevent so that the server can play the animation
	mouse.Icon = "http://www.roblox.com/asset?id=90980978"
	mouse.Button1Down:Connect(function()
		if bullets.Value <=0 or reloading == true then
			-- Don't do anything
		else
			--find which directino head is looking as CFrame..note: no period. between "Workspace and [player.Name]..
			local head = game.Workspace[player.Name].Head.CFrame.lookVector
			--finds head position cframe, then finds mouse.hit position, and finds the look vector of the mouse with this info as cframe
			local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
			--difference was initiated to "nil" at start of script
			--uses above two lines, and finds difference between head and mouse,
			--allowing you to see if you are trying to fire behind yourself, less than 1.33 is OK
			difference = (head-mouse)
			--raycast script below - create a ray. start point:(tool.handle.cframe.p) endpoint:(mouse hit position minus tool handle position) times number of studs 
			local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*300)
			--findpartonray script below
			local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
			--plays gunfire sound
			sound:Play()
			--uses info above to check if character is shooting behind himself, and only shoots if sideways or front shooting
			--allows character to shoot if 'difference' is less than 1.33 
			--magnitude proprty is the difference between two points
			if difference.magnitude < 1.33 then
				--this calls remoteevent 'shootevent' from replicated storage and passes arguments 'tool' 'position' and 'part'
				shootevent:FireServer(tool,position,part)
				--removes a bullet from inventory ..'bullets' is a variable referencing a global number value
				--hence the property change (52m50s into video at this point)
				bullets.Value = bullets.Value - 1
			end
		end
	end)
	--creates reload button for mobile users that do not have a keyboard
	--arguments:(buttons name, function called, do we want a button to show on screen, what will button be?)
	local reloadMobileButton = contextActionService:BindAction("ReloadBtn",reload,true,"r")
	--sets position of button for mobile users
	contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
	--sets image of reload button
	contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419")
end)

--this function called when tool unequipped
tool.Unequipped:Connect(function()
	--get rid of gun mouse icon, and replace with standard mouse icon
	mouse.Icon = ""
	--fires server event to run unequip animation, passes in parameter of the animation tool.shoot because that
	--animation was started when the gun was equipped, and it will be stopped when the gun is unequipped, so we have
	--to pass it through to the server as well as an argument
	unequipanimation:FireServer(tool.shoot)
	--destroy the gunGUI since gun is unequipped (wouldn't it be better to reomve it?
	player.PlayerGui.GunGUI:Destroy()
	--gets rid of reload button
	contextActionService:UnbindAction("ReloadBtn")
end)

--this is a client event awaiting firing by the main script on the server
headshot.OnClientEvent:Connect(function()
	--get player and players gunGUI for the headshot GUI, and then tween the headshot label from its first position, the second position. the last 3
	--arguments control the tween effect
	player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(0.5,-100,0.5,-25), "Out","Quint",0.3)
	--keeps it on screen for 1.5sec
	wait(1.5)
	--tweens it offscreen again to right
	player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(-1,0,0.5,-25), "In","Quint",0.4)
	wait(0.5)
	--resets to original position offscreen at left
	player.PlayerGui.GunGUI.Headshot.Position = UDim2.new(1.5,0,0.5,-25)
end)

--

main script can be posted too if needed

All help appreciated Thanks

5 comments

0

Here's the main script:

0

Don't use alvins tutorials, there garbage and are literally copied from other users.

0

Don't go around posting "alvinblock script no work help xd lol?", go create your own scripts.

0

imagine moderating year old question

0

Fragmentation123 - Your response was useless. To create your own scripts, you gotta learn to script. To learn to script, you have to use the resources available. AlvinBlox is just one resource. Don't be so useless in your responses. You wasted everyone's time with that.

1 answer

Here's the main script:

MainScript:

--Main Script which goes in Server Storage --]]

--starts scripting on video at 1h 15m
--
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
--Kills is spelled exactly the same as it is on leaderboard in serverscriptservice
local KOValue = "Kills"
--Wipeouts is spelled same as on leaderboard serverscriptservice
local WOValue = "Wipeouts"
--
local damage = 30

--this event is waiting to be triggered by client
--these events are located in replicated storage
--it is called by local script when mouse1down is pressed to shoot gun
--parameters are same that were sent by the client calling this
--except for 'player' because roblox automatically send that paramater
--the next three after player were passed by client
replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
	--checks to see if player is still alive first
	if game.Workspace[player.Name].Humanoid.Health <= 0 then
		-- do nothing - player dead
	else
		--=determine how far ray is traveling
		local distance = (tool.Handle.CFrame.p - position).magnitude
	--check if player has already shot, and already has a bullet trail hangingi n air
	--bullet trail is called 'trajectory' .. have to have it spelled just write
	--first line checks if it exists
		if game.Workspace:FindFirstChild(player.Name.."'s Trajectory") then
			--this line destroys it
			game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy()
		end
		--trajectory is a bullet path that holds in air to show where bullet went
		--isn't really there - just an effect
		--create a part named 'trajectory' and sets it parent to workspace (second argument)
		local trajectory = Instance.new("Part",game.Workspace)
		--go to serverstorage, and insertobject into serverstorage 'particle emitter'
		--edit its properties to look like smoke
		--then reference it with the line below
		local smoke = serverStorage.SmokeParticle:Clone()
		--set the parent to the tool handle
		smoke.Parent = tool.Handle
		--sets the color,etc of the brick that will be the smoke
		trajectory.BrickColor = BrickColor.new("Institutional white")
		trajectory.Material = "SmoothPlastic"
		--sets name of bullet trail to playername's Trajectory  --clever!
		trajectory.Name = player.Name.."'s Trajectory"
		trajectory.Transparency = 0.5
		trajectory.Anchored = true
		trajectory.Locked = true
		trajectory.CanCollide = false
		--change size of brick to proper smoketrail size
		trajectory.Size = Vector3.new(0.3,0.3,distance)
		--bullet trail hangs in air for a second afer shot
		--explains this at 1h 35m on video
		--
		for i = 0,distance,6 do
			trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2)
			--short wait allows us to see change happenning
			wait(0.0001)
		end
		--destroy the smoke
		smoke:Destroy()
		
		--did we hit a part?
		if part then
			--if the part we hit was a head or a hat, and if the humanoid attached is alive still..then
			if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then
				-- Boom headshot
				--server accesses replicated storage, so that it can do something on the client
				--so it uses fireclient and passes argument player
				replicatedStorage.Headshot:FireClient(player)
				--damages player for 50 hp for headshot
				damage = 50
			end
			--target was not head, so now we look for humanoid of the part shot
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			--if it couldn't find a humanoid in the parent, it checks the parent of the parent for a humanoid
			--in case it hit something housed inside something else
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			else
				--does damage to humanoid, hedshot if statement above sets damage to 50 if headshot
				--otherwise it remains 30 as stated at top
				humanoid:TakeDamage(damage)
				--after taking damage, checks to see if humanoid still alive`
				if humanoid.Health <= 0 then
					--increases kill count on leaderboard in serverscriptservice
					--for successful kill
					player.leaderstats[KOValue].Value = player.leaderstats[KOValue].Value + 1
					--increases wipeout count for player vanquished on leaderboard in serverscriptservice
					game.Players[humanoid.Parent.Name].leaderstats[WOValue].Value = game.Players[humanoid.Parent.Name].leaderstats[WOValue].Value + 1
				end		
			end	
			
			wait(0.25)
			
			--checks if there is a trajectory still and if there is one it destroy it
			if trajectory then
				trajectory:Destroy()
			end
		end
	end
	
	--ends shootevent 
end)



replicatedStorage.EquipAnimation.OnServerEvent:Connect(function(player,animation)
	local newAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
	newAnim:Play()
	replicatedStorage.UnequipAnimation.OnServerEvent:Connect(function(player,animation)
		newAnim:Stop()
		for i,v in pairs(game.Workspace:GetChildren()) do
			if v.Name == player.Name.."'s Trajectory" then
				v:Destroy()
			end
		end
	end)
	replicatedStorage.Reload.OnServerEvent:Connect(function(player,animation)
		newAnim:Stop()
		local reloadAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
		reloadAnim:Play()
	end)
end)

function checkBodyType(player,tool)
	if game.Workspace[player.Name]:FindFirstChild("LowerTorso") then -- R15
		tool.shoot.AnimationId = "http://www.roblox.com/asset/?id=1441315431"
		tool.reload.AnimationId = "http://www.roblox.com/asset/?id=1441274016"
		return "R15"
	end
	if game.Workspace[player.Name]:FindFirstChild("Torso") then -- R6
		tool.shoot.AnimationId = "http://www.roblox.com/asset/?id=1441256046"
		tool.reload.AnimationId = "http://www.roblox.com/asset/?id=1438081223"
		return "R6"
	end
end
replicatedStorage.CheckBodyType.OnServerInvoke = checkBodyType
--

thanks again

This discussion is preserved as it was. New answers, comments, and votes are closed.