Server Side Animations (help)

I have been trying to make a gun and it was all going quite smoothly – at least until I began scripting the animations. I have seen Alvin_Blox script animations in his gun tutorial, and saw that he used remote events to control the animation as animations do not seem to replicate between client and server. So, Seeing this, I decided to use remotes as well. The problem is that the animations do not play/start. I have looked within the scripts of other guns to attempt to troubleshoot the issue. I have consulted many scripters with no further luck. I have provided some information below regarding the figurative roadblock.

Server Script (Located in a folder, in the gun tool)

Open
local remotes = script.Parent.Parent:WaitForChild("Remotes")
local shootEvent = remotes:WaitForChild("Shoot")
local sound
local reloadEvent = remotes:WaitForChild("Reload")
local idleEvent = remotes:WaitForChild("IdleAnimation")

local reloadAnim
local idleAnim
local shootAnim

local function reload(player)
	if not reloadAnim then
		reloadAnim = player.Character.Humanoid:LoadAnimation(script:WaitForChild("Reload"))
	end
	reloadAnim:Play()
	print("RELOAD")
	return true
end

local function idle(player, bool)
	print("IDKLE")
	if not idleAnim then
		idleAnim = player.Character.Humanoid:LoadAnimation(script:WaitForChild("Idle"))
	end
	
	if bool then
		idle:Play()
	else
		idleAnim:Stop()
	end
	print("IDle!")
	return true
end

local function shoot(player)
	print("SHOOT")
	if not shootAnim then
		shootAnim = player.Character.Humanoid:LoadAnimation(script:WaitForChild("Shoot"))
	end
	shootAnim:Play()
	return true
end

local function createPart(barrelPosition, mouseLocation)
	local part = Instance.new("Part")
	part.Size = Vector3.new(0.1, 0.1, 3)
	part.CFrame = CFrame.new(barrelPosition, mouseLocation)
	--part.Parent = script.Parent.Parent
	part.Color = Color3.new(1, 0.52549, 0.137255)
	part.Material = Enum.Material.Neon
	part.Anchored = false
	part.CanCollide = false
	
	return part
end

local function onEvent_shootEvent(player, hit, position, mousePosition)
	--create bullet
	local barrel = script.Parent.Parent.Barrel
	local bullet = createPart(barrel.Position, mousePosition)
	local bv = Instance.new("BodyVelocity", bullet)
	bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
--	bv.P = 1200
	bv.Velocity = bullet.CFrame.LookVector * 300

	if not script.Parent.Parent.Barrel:FindFirstChild("Sound") then
		sound = script:WaitForChild("Shot"):Clone()
		sound.Name = "Sound"
		sound.Parent = script.Parent.Parent.Barrel
	end
	sound:Play()
	
	shoot(player)
	print("SHOT")
	
	bullet.Transparency = 1
	bullet.Parent = workspace.Bullets
	wait(0.2)
	bullet.Transparency = 0
	
	game:GetService("Debris"):AddItem(bullet, 0.5)
	
	bullet.Touched:Connect(function(hit)
		if not script.Parent.Parent:FindFirstChild(hit) or player.Character:FindFirstChild(hit) then
			local playerTwo = game.Players:GetPlayerFromCharacter(hit.Parent)
			if playerTwo then
				if playerTwo.Team ~= player.Team then
					if hit.Name == "Head" then
						playerTwo.Character.Humanoid.Health:TakeDamage(50)
					else
						playerTwo.Character.Humanoid.Health:TakeDamage(20)
					end
				end
			end
			bullet:Destroy()
		end
	end)
end

reloadEvent.OnServerEvent:Connect(reload)
idleEvent.OnServerEvent:Connect(idle)
shootEvent.OnServerEvent:Connect(onEvent_shootEvent)

local script (Located in a folder, in the gun as well)

Open
local remotes = script.Parent.Parent:WaitForChild("Remotes")
local shootEvent = remotes:WaitForChild("Shoot")
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local reloadTime = 2

local gunUi = script:WaitForChild("GunUI")

local values = script.Parent.Parent.Configuration
local bullets = values.Bullets
local maxBullets = values.MaxBullets

local function reload()
	if not script.Parent.Parent:FindFirstChild("GunUI") then
		local screenGui = player.PlayerGui:FindFirstChild("GunUI") 
		if screenGui then
			remotes:WaitForChild("Reload"):FireServer()
			local frame = screenGui:WaitForChild("Main")
			local bulletsLabel = frame.Bullets
			local title = frame.Title
			
			title.Text = "Reloading"
			wait(reloadTime)
			title.Text = "Bullets"
			bullets.Value = maxBullets.Value
			return true
		end
	end
end

local function updateBullets()
	local screenGui = player.PlayerGui.GunUI 
	local frame = screenGui:WaitForChild("Main")
	local bulletsLabel = frame.Bullets
	local title = frame.Title
	
	bulletsLabel.Text = tostring(bullets.Value) .. "/" .. tostring(maxBullets.Value)
end

local function getMouseLocation()
	local mousePosition = userInputService:GetMouseLocation()
	local x = mousePosition.X
	local y = mousePosition.Y
	
	local unitray = workspace.CurrentCamera:ViewportPointToRay(x, y, 0)
	local ray = Ray.new(unitray.Origin, unitray.Direction * 200)
	local target, position = workspace:FindPartOnRayWithIgnoreList(ray, {player.Character, script.Parent.Parent})
	
	return position
end

local function equip()
	remotes:WaitForChild("IdleAnimation"):FireServer(true)
	print("FIRED")
	gunUi.Parent = player.PlayerGui
	player:GetMouse().Icon = "http://www.roblox.com/asset/?id=5189224891"
end

local function unequip()
	remotes:WaitForChild("IdleAnimation"):FireServer(false)
	player.PlayerGui.GunUI.Parent = script
	player:GetMouse().Icon = ""
end

local function shoot()
	if bullets.Value > 0 then
		local mouseLocation = getMouseLocation()
		shootEvent:FireServer(nil, nil, mouseLocation)
		bullets.Value -= 1
	else
		reload()
	end
	
end

game:GetService("RunService").RenderStepped:Connect(updateBullets)
script.Parent.Parent.Activated:Connect(shoot)
script.Parent.Parent.Equipped:Connect(equip)
script.Parent.Parent.Unequipped:Connect(unequip)

Image of my Explorer

Open

Video of me using the gun

Open

robloxapp-20211108-1451252.wmv (825.7 KB)

  • It seems that the print() functions are not printing anything.
1 Like

Play the animations locally, they will replicate to the server.

2 Likes