Need help with a weapon script

Hello there,

I’m trying to make a weapon called a “Tranquillizer”. It’s a weapon where if you shoot to a enemy, the enemy will fall asleep for 20 second. But for some reason it doesn’t work :confused: I hope you guys can help. Let me know if you want more detail

Tranquillizer(Script)
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

--//Variables
local Events = ReplicatedStorage:WaitForChild("Events")
local RoundInfo = ReplicatedStorage:WaitForChild("RoundInfo")
local Billboards = ReplicatedStorage:WaitForChild("Billboards")

local lastOwner = nil
local stunned = false
local fireSound = script.fire_sound
local sleepAnim = script.Sleep

--//Functions
local function WeaponHandler(newOwner, weapon)
	if Players:GetPlayerFromCharacter(newOwner) then
		local player = Players:GetPlayerFromCharacter(newOwner)
		Events:WaitForChild("toggleWeaponHolder"):FireClient(player, weapon, true)
		lastOwner = player
	else
		if lastOwner and Players:FindFirstChild(lastOwner.Name) then
			Events:WaitForChild("toggleWeaponHolder"):FireClient(lastOwner, weapon, false)
			lastOwner = nil
		end
	end
end

local function AddBillboard(object, orderType)
	local orderedBillboard = Billboards:FindFirstChild(orderType)
	if orderedBillboard then
		local billboard = orderedBillboard:Clone()
		billboard.Parent = object
		billboard.Adornee = object
		billboard.Enabled = true
	end
end

local function Countdown(duration, trap)
	for i=duration,0,-0.5 do
		--print(i)
		local billboard = trap:FindFirstChild("TimerGui")
		if billboard and billboard:FindFirstChild("Status") then
			billboard.Status.Text = i
		else
			print(nil)
		end
		wait(0.5)
	end
	wait(0.5)
end

local function StunKiller(killer, weapon)
	if stunned == false then
		stunned = true
		
		weapon:WaitForChild("AmmoValue").Value = weapon:WaitForChild("AmmoValue").Value - 1
		
		local freezeBindable = killer:FindFirstChild("Freeze")
		if freezeBindable then
			freezeBindable:Fire("stun", 20)
		end
		
		local sound = fireSound:Clone()
		sound.Parent = killer.Head
		sound:Play()
		
		local humanoid = killer.Humanoid
		local sleepTrack = humanoid:LoadAnimation(sleepAnim)
		sleepTrack:Play()
		
		Events:WaitForChild("toggleMessage"):FireAllClients("Sleep", killer)
		
		AddBillboard(killer.Head, "TimerGui")
		killer.Head:WaitForChild("TimerGui").ExtentsOffsetWorldSpace = Vector3.new(0,2,0)
		
		Countdown(20, killer.Head)
		
		killer.Head:WaitForChild("TimerGui"):Destroy()
	end
end

local function New(weapon)
	local ammo = Instance.new("IntValue")
	ammo.Name = "AmmoValue"
	ammo.Value = 3
	ammo.Parent = weapon
	
	ammo.Changed:Connect(function()
		if ammo.Value == 0 then
			weapon:Destroy()
		end
	end)
	
	weapon.AncestryChanged:Connect(function(child, parent)
		WeaponHandler(parent, weapon)
	end)
	
	Events:WaitForChild("toggleStunned").OnServerEvent:Connect(function(player, target)
		if target and
			CollectionService:HasTag(target, "KillerBot") and
			CollectionService:HasTag(target, "KillerPlayer")
		then
			if ammo.Value ~= 0 then
				local killer = target
				StunKiller(killer, weapon)
			else
				Events:WaitForChild("toggleWeaponHolder"):FireClient(lastOwner, nil, false)
			end
		end
	end)
end

--//Run functions
CollectionService:GetInstanceAddedSignal("Tranquillizer"):Connect(function(obj)
	if obj:FindFirstAncestor("Game") then
		local weapon = obj
		print("Tag added")
		New(weapon)
	end
end)
WeaponClient(LocalScript)
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")

--//Variables
local Events = ReplicatedStorage:WaitForChild("Events")
local Modules = ReplicatedStorage:WaitForChild("Modules")

local player = Players.LocalPlayer
local mouse = player:GetMouse()

local firing = false
local remainAmmo = 3
local reloadDur = 3
local tranquillizer = nil
local equipping = false

local gameGui = script.Parent
local weaponGui = gameGui.Tranquillizer

--//Functions
local function ToogleHolder(weapon, equip)
	if equip then
		print("Equipped")
		
		equipping = true
		tranquillizer = weapon
		
		weaponGui.Visible = true
		weaponGui.Ammo.Text = weapon:FindFirstChild("AmmoValue").Value.."/3"
		
		weapon:WaitForChild("AmmoValue").Changed:Connect(function()
			weaponGui.Ammo.Text = weapon:WaitForChild("AmmoValue").Value.."/3"
			remainAmmo = weapon:WaitForChild("AmmoValue").Value
		end)
		
	else -- false
		print("Unequipped")
		
		equipping = false
		tranquillizer = nil
		
		weaponGui.Visible = false
	end
end

local function FireWeapon()
	print("Clicked")
	if equipping == true then
		if remainAmmo ~= 0 then
			if firing == false then
				firing = true
				
				local character = player.Character
				local ray = Ray.new(tranquillizer.Barrel.CFrame.p, (mouse.Hit.p - tranquillizer.Barrel.CFrame.p).Unit*600)
				local hit, pos = workspace:FindPartOnRay(ray, character, false, true)
				
				local beam = Instance.new("Part", workspace.Game)
				beam.Name = "Beam"
				beam.BrickColor = BrickColor.new("Brick yellow")
				beam.Material = Enum.Material.Neon
				beam.Transparency = 0.5
				beam.Anchored = true
				beam.Locked = true
				beam.CanCollide = false
				
				local distance = (tranquillizer.Barrel.CFrame.p - pos).Magnitude
				beam.Size = Vector3.new(0.2,0.2,distance)
				beam.CFrame = CFrame.new(tranquillizer.Barrel.CFrame.p, pos) * CFrame.new(0,0,-distance/2)
				
				Debris:AddItem(beam, 0.05)
				
				if hit and hit:IsA("BasePart") then
					local killer = hit.Parent
					
					if killer then
						Events:WaitForChild("toggleStunned"):FireServer(killer)
					end
					
				end
				
				wait(reloadDur)
				firing = false
				
			end
			
		end
	end
end

--//Run functions
Events:WaitForChild("toggleWeaponHolder").OnClientEvent:Connect(function(weapon, equip)
	ToogleHolder(weapon, equip)
end)

mouse.Button1Down:Connect(function()
	FireWeapon()
end)

Cheers,
The_UncleDev

Are there any errors in the output?

there are no error and no warn message

We really need more info on what’s not working. Have you tried printing? If so, tell us where it doesn’t print.

I tried printing in every if line and fix some script and now it’s working

1 Like