Animation Playing on Wrong Player

robloxapp-20200710-1818486.wmv (3.3 MB)
.

Code inside shotgun (LocalScript):

local Tool = script.Parent --make sure this is a Tool object
 
Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		script.Parent.Handle.FireSound:Play()
		getMouseTarget()
	end)
end)

function getMouseTarget()
	local cursorPosition = game:GetService("UserInputService"):GetMouseLocation()
	local oray = game.workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)
	local ray = Ray.new(game.Workspace.CurrentCamera.CFrame.p,(oray.Direction * 1000))
	
	Checker(ray)
end

function Checker(ray)
	local a = workspace:FindPartOnRay(ray)
	local b
	for i = 1, 250 do wait()
		if a then -- Makes sure there's a Ray to begin with,
			if a:FindFirstChild("Jason") ~= nil then -- Checks to see if the Ray has the required value in it,
				b = a["Jason"] -- Sets the value,
				game.Workspace.ShotgunStun:FireServer(a)
				break
			else
				a = a.Parent -- If the Ray or current 'a' doesn't have the desired Value in it, it'll work it's way up through the parents.
			end
		end
	end
end

.
Code inside Workspace (Script):

local function stun(a)
	local model = game.Workspace:FindFirstChild(a.Name)
	local animation = game.Workspace:WaitForChild('ShotgunStunAnim')
	local humanoid = model:WaitForChild('Humanoid')
	local dance = humanoid:LoadAnimation(animation)
	humanoid.WalkSpeed = 0
	dance:Play()
	wait(dance.Length)
	humanoid.WalkSpeed = 16
end

workspace.ShotgunStun.OnServerEvent:Connect(stun)

.

Player is in third person.

1 Like

you are are using workspace:FindPartOnRay() which does not have any raycast params, this means that the raycast is hitting your character’s head. Instead you could use workspace:RayCast() which you can define parameters that will prevent it from hitting you.
here is how I think the script should work

local params = RaycastParams.new()
params.FilterDescendantsInstances = {game.Players.LocalPlayer.Character} 
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = false

local Tool = script.Parent
 
Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		script.Parent.Handle.FireSound:Play()
		getMouseTarget()
	end)
end)

function getMouseTarget()
	local cursorPosition = game:GetService("UserInputService"):GetMouseLocation()
	local oray = game.workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)
	local ray =game.Workspace.CurrentCamera.CFrame.Position,oray.Direction * 1000, params
	
	Checker(ray)
end

function Checker(ray)
local a = workspace:Raycast(ray)
if a then
if a.Instance:IsDecendentOf(workspace.Jason) then --this will find whether the part the ray hit is a descendent of 'Jason'
game.Workspace.ShotgunStun:FireServer("Jason")
elseif a.Instance:IsDecendentOf(workspace.Jason2) then --this will find whether the part the ray hit is a descendent of 'Jason2' you could continue this to 3 or or 4 or 5, it doesn't matter how many Jasons you have as long as you can define their name.
game.Workspace.ShotgunStun:FireServer("Jason2")
end
end
end

I Hope this works! (you may have to readd the thing about the ray moving up through it’s parents, but I’m not sure what you meant by that) I had this exact same problem, if this doesn’t fix it you coulld always use print(ray.Instance.Name)

1 Like

The ray moving up through the parents means that inside the character model there is a BoolValue named Jason (value doesn’t matter because it is only inside the Jason character), and, if found, then the animation will trigger.
Post about it here: Get Higher Ancestor

I had a feeling it was triggering because of the character with the item but I wasn’t sure as everything worked fine before I made it use a RemoteEvent so the animation would show up to everyone.

I have just changed the script to work better, as long as you can define every Jason’s Name with an added elseif to the checker it should work perfectly fine

It’s a little tedious, but it’ll work great. Thanks!

1 Like

@BluePaint_Games
there’s an error on the line

	local a = workspace:Raycast(ray)

saying

Argument 2 missing or nil

and I don’t know much about raycasting so I don’t know what to do.

this means there is a problem with the second arguement (oray.Direction * 1000) im not sure what to do but i’ll get back to you (sorry for late response, I’m busy)

I’ve Fixed up the code a bit, but you’ll HAVE to put one in each Jason or It won’t work

local params = RaycastParams.new()
params.FilterDescendantsInstances = {game.Players.LocalPlayer.Character} 
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = false

local Jason = script.Parent
local Tool = game.Players.LocalPlayer.Backpack:FindFirstChild(name of tool here) --you'll have to put the name of tool in here

local subjecthead = script.Parent.Head
local castfrom = Tool.Handle
 
Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
local mouse = script.Parent.Parent:GetMouse()
		Tool.Handle.FireSound:Play()
		if mouse.Target == "Jason" then
workspace.Jason.RemoteEvent:FireClient()
elseif mouse.Target == "Jason2" then --etc etc

end
end
	end)
end)
-- tool.equipped will have to be inside the tool, so you will have to use a remote event

local function onevent()
getMouseTarget()
end
script.Parent.RemoteEvent.OnClientEvent:Connect(onevent)

function getMouseTarget()
	local oray = subjecthead.Position - castfrom.Position
	local ray =Tool.Handle.Position, oray.unit * 1000, params
	
	Checker(ray)
end

function Checker(ray)
local a = workspace:Raycast(ray)
if a then
if a.Instance:IsDecendentOf(Jason) then --this will find whether the part the ray hit is a descendent of 'Jason'
game.Workspace.ShotgunStun:FireServer(Jason.Name)
end
end
end

Sorry this took long to get back to you, If this doesn’t work just tell me, raycasting is a bit finicky, you will have to specify the name of the shotgun tool inside of the character’s backpack.

EDIT: Changed the script a small bit, with the previous one it would fire on all jasons if the raycasting made it

Interesting. I don’t see how it would make Jason be stunned though when the ray is coming from someone else.

Side note: Should this be a LocalScript because it uses LocalPlayer?

I’ve used rays like this before, that’s how I know it will likely work, and yes it should be a localscript as I see no reason not to. It also NEEDS to be a child of Jason and it does check whether the ray from the shotgun hits its parent (Jason).