Unable to cast to Dictionary with MousePos

Hello (again)

I have a problem with my gun, I am tweening a bullet from the position of the barrel, to the mouse.Hit.p (mouse position). For some reason I cannot get it to tween because of the same old error "Unable to cast to Dictionary), I want to do this so I can make bullet trails and bullet whizzing.

The bullet doesn’t even show up for some reason… Here is my code, if you need me to shorten it for you then I will do that: (Both local script and serverscript)

Localscript:

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local UIS = game:GetService("UserInputService")

local tool = script.Parent

local equip = tool:WaitForChild("Handle"):WaitForChild("Equip")
local EAnim = char:WaitForChild("Humanoid").Animator:LoadAnimation(equip)

local idle = tool:WaitForChild("Handle"):WaitForChild("Idle")
local IAnim = char:WaitForChild("Humanoid").Animator:LoadAnimation(idle)

local fire = tool:WaitForChild("Handle"):WaitForChild("Shoot")
local FAnim = char:WaitForChild("Humanoid").Animator:LoadAnimation(fire)

local reload = tool:WaitForChild("Handle"):WaitForChild("Reload")
local RAnim = char:WaitForChild("Humanoid").Animator:LoadAnimation(reload)

local Ammo = tool.Ammunition

local debounce = true

local AnimationDebounce = true

local Mouse = plr:GetMouse()

local Looping = true

tool.Equipped:Connect(function()
	EAnim:Play()
	wait(1)
	IAnim:Play()
	EAnim:Stop()
	Mouse.Button1Down:Connect(function(mouse)
		if debounce and Ammo.Value > 0 then
			Looping = true
			while Looping do
				if Ammo.Value <= 0 then
					return
				else
					FAnim:Play()
					Ammo.Value = Ammo.Value - 1
					print(Ammo.Value)
					tool.Handle.FSound:Play()
					local mousepos = Mouse.Hit.p
					tool.Fired:FireServer(mousepos)
					wait(0.1)
					debounce = true
				end
			end
		elseif Ammo.Value <= 0 then
			Looping = false
			Ammo.Value = Ammo.Value
			tool.Handle.out:Play()
		end
	end)
end)


Mouse.Button1Up:Connect(function()
	Looping = false
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		if Ammo.Value >= 0 and AnimationDebounce == true then
			AnimationDebounce = false
			task.wait(0.3)
			RAnim:Play()
			tool.Handle.RSound:Play()
			wait(2)
			AnimationDebounce = true
			Ammo.Value = 32
			Looping = true
		end
	end
end)


tool.Unequipped:Connect(function()
	wait()
	IAnim:Stop()
	FAnim:Stop()
	repeat wait() until IAnim:Stop()
	Looping = false
end)

Server:

local looping = true

local tool = script.Parent

local TweenService = game:GetService("TweenService")

local bullet = game.ServerStorage.Bullet

tool.Fired.OnServerEvent:Connect(function(plr, mouse)
	
	local whiz = bullet:Clone()
	
	whiz.Parent = game.Workspace
	
	whiz.CFrame = CFrame.new(tool.RaycastPos.Position)
	
	whiz.Touched:Connect(function(hit)
		local char = plr.Character
		local parent = tool.Parent
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= parent then
			hum:TakeDamage(20)
		end
		
		whiz:Destroy()
	end)
	
	local tweeninfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	
	local goal = mouse
	
	local Fired = TweenService:Create(whiz, tweeninfo, goal)
	
end)

I know using this is kinda weird but its what I was figuring out and what I came up to think of.

If you guys can help me that would be great.

local goal = {Position = mouse}
local Fired = TweenService:Create(whiz, tweeninfo, goal)

The value passed as an argument to the goal parameter of TweenService:Create() is expected to be in the form of a dictionary, with the keys/indices representing properties and the values representing the goals of those properties.

1 Like

There’s no error anymore but on line 20 I get this error:
Players.squaredsnow.Backpack.MP40.Script:20: attempt to index nil with ‘FindFirstChild’ - Server - Script:20

Add if hit.Parent then before it’s indexed.

Players.squaredsnow.Backpack.MP40.Script:22: attempt to index nil with ‘TakeDamage’

Idk why its saying my :TakeDamage() function is nil

local hum = hit.Parent:FindFirstChild("Humanoid")
if hum and hit.Parent ~= parent then
	hum:TakeDamage(20)
end

With the if hit.Parent then before it.

1 Like

Players.squaredsnow.Backpack.MP40.Script:20: attempt to index nil with ‘FindFirstChild’

I don’t get why my studio keeps making these kinds of errors for every little thing. I spend hours just trying to fix nil errors :agony:

Yeah, that means hit.Parent is nil, or whatever you’re calling FindFirstChild() on.

Just add checks for each instance before you attempt to index it/call methods on it.

Alright, also it still wont tween, how do i fix that?

You need to call :Play() on the created tween.

Fired:Play()

I’m dumb, sorry lol. Lemme fix that right now