Having trouble with making a gun system

The mouse instance is locked only to the playerfor this you need to use remoteevents

I would suggest doing:

This is the serverscript:

local tool = script.Parent
local anim1 = Instance.new("Animation")
anim1.AnimationId = "http://www.roblox.com/Asset?ID=13694221900" --Animation ID
local anim2 = Instance.new("Animation")
anim2.AnimationId = "http://www.roblox.com/Asset?ID=13694242858"
local track
local FireGun = script.Parent:FindFirstChild('FireGun')
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local Model = Mouse.Target:GetRootPart().Parent
FireGun:FireServer(Model)
local Remotefunction = -- make a remote function somewhere
local Damage = 40

FireGun.OnServerEvent:Connect(function(Player, Target)
	local Humanoid = Target:FindFirstChild('Humanoid')
	if Humanoid then
		Humanoid:DealDamage(Damage)
	end
end)

local function idleanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function fireanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
	local playercharactermodel = Remotefunction:InvokeClient(game.Players:GetPlayerFromCharacter(tool.Parent)
end

local function toolEquipped()
	tool.Handle.Equip:Play()
	idleanim()
end

local function toolActivated()
	tool.Handle.Fire:Play()
	fireanim()
	tool.Handle.PointLight.Enabled = true
	wait(0.1)
	tool.Handle.PointLight.Enabled = false
	track:Stop()
	idleanim()
end

local function toolUnequipped()
	if track then
		track:Stop()
	end
end

tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(toolUnequipped)

this is the local

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local Remotefunction = -- make a remote function somewhere

function FindCharacterFromTarget()
	local Model = Mouse.Target:GetRootPart().Parent
	local Humanoid = Model:FindFirstChildOfClass('Humanoid')

	if Humanoid then
		return Model
	end
end

Remotefunction.OnClientInvoke = FindCharacterFromTarget

It seems to me like you’ve misunderstood how replication works, and mixed up where you’ve placed the code I’ve given you:

  1. LocalPlayer cannot be used in a serverscript, and neither can you use GetMouse on the server
  2. OnServerEvent is a serverside event which cannot be called from the client
  3. You should swap your entire serverscript code out for this:
local Tool = script.Parent

local FireGun = script.Parent:FindFirstChild('FireGun')
local Damage = 25

local Equipped = false -- This is for sanity checks to prevent unwanted behaviour through exploiting
Tool.Equipped:Connect(function()
	Equipped = true
end)

Tool.Unequipped:Connect(function()
	Equipped = false
end)

FireGun.OnServerEvent:Connect(function(Player, Target)
	if not Equipped then
		return 
	end
	local Humanoid = Target:FindFirstChild('Humanoid')
	if Humanoid then
		Humanoid:TakeDamage(Damage)
	end
end)

@Qinrir I personally would not handle tool events on the server. Similarly, animations shouldn’t be played on the server, either. Animations are automatically replicated if they are played on the player’s character.

The final LocalScript would be:

local tool = script.Parent
local anim1 = Instance.new("Animation")
anim1.AnimationId = "http://www.roblox.com/Asset?ID=13694221900" --Animation ID
local anim2 = Instance.new("Animation")
anim2.AnimationId = "http://www.roblox.com/Asset?ID=13694242858"
local track

local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()

local FireGun = script.Parent:FindFirstChild('FireGun')

function FindCharacterFromTarget()
	local Model = Mouse.Target:GetRootPart().Parent
	local Humanoid = Model:FindFirstChildOfClass('Humanoid')

	if Humanoid then
		return Model
	end
end

local function idleanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function fireanim()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
end

local function toolEquipped()
	tool.Handle.Equip:Play()
	idleanim()
end

local function toolActivated()
	local OtherPlayer = FindCharacterFromTarget()
	if OtherPlayer then
		FireGun:FireServer(OtherPlayer) -- Replicate damage dealt
	end
	tool.Handle.Fire:Play()
	fireanim()
	tool.Handle.PointLight.Enabled = true
	wait(0.1)
	tool.Handle.PointLight.Enabled = false
	track:Stop()
	idleanim()
end

local function toolUnequipped()
	if track then
		track:Stop()
	end
end

tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(toolUnequipped)

The scripts don’t work. Maybe it is to do with how I set it up here is an image:
image

Do you have any errors in the output?

No I do not have any errors. I have even tried to fix it but nothing has been working.

Upon further reading of the script I have a question. Does this script only work against players or can it damage NPCs? If it can’t damage NPCs then that is the issue as I have been testing it on NPCs.

This code should work with NPC’s too, as long as you have set a PrimaryPart for your NPC models. I’ll have a look at what the problem is and what you may have done wrong. I’ll update this post in roughly 30 minutes.

Edit:
Something I discovered: I had accidentally written Humanoid:DealDamage instead of Humanoid:TakeDamage. Other than that, this is working fine for me. I didn’t have your animations, so I used prints in place of an animation.

@Director_Soda

1 Like

Thank you it works now!! I could not do this without you thank you for your amazing help.

1 Like

I’m glad I was able to help :grin: Best of luck!

1 Like

Can you give any advice to be able to walk like this?

1 Like

I would love to! Could you please DM me on here so I don’t clog up this post with off-topic discussions? I’ll get back to you as soon as possible. :grin:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.