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:
LocalPlayer cannot be used in a serverscript, and neither can you use GetMouse on the server
OnServerEvent is a serverside event which cannot be called from the client
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)
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.