Help with an error

Hey, I’m trying to make a tool that can get throwed and its direction is determined by your mouse but I got this error: attempt to index nil with ‘Character’

Any Fixes?

Server script:

Local Script:

Location:
image

2 Likes

player is nil. I don’t know how you defined it, but if you used Players.LocalPlayer, it doesn’t work on the server

2 Likes

i didnt use players.localplayer on the server

1 Like

In that screenshot, at the very top is what he’s talking about

1 Like

Now seems like a very good time to elaborate on what you did use

1 Like

How did you define player in the server script?

1 Like

I may be wrong, but I think you’re firing the remote event wrong, you’re meant to locate the player first.

1 Like

I used this piece of code
image

(the script in is the players backpack because its in starter player

1 Like

Get rid of

.Character

, character is only used for finding a character in game.Players

I also recommend since you’re using a remote event, to just get the player that way rather than using that script

so how would define the character now?

It seems you managed to access the parent of the runtime data model (game), which does not exist. You likely misjudged the relative location of the Player instance with respect to the location of your script

You already defined it since the tool is in the player and is already in the character, using too many .Parent is going to lead to workspace and most likely nothing is named “Character” in workspace

The error is not suggesting that the character is non-existent, it is suggesting that what we’re attempting to index the character from is

You’re withholding the whole script, as well as context about its location. Knowing that it will eventually be a descendant of a player’s backpack, you can use this surefire method for accessing that player:

local player = script:FindFirstAncestorOfClass("Player")

The better you understand the player’s relative location, the less distance you need to travel to retrieve it. For example, you could begin the search from the tool instead of the script

I wasn’t suggesting that the character is non-existence, I was trying to say that using Character isn’t needed since the tool is already in the Character and the script is trying to locate something named Character

On another note, this script is vulnerable to exploitation; an exploiter could place the tool’s copy at any point within the world, which, given what you’re doing with the remote, will enable flinging and aim-bot. If your script is located inside of the handle, it can also enable spam

1 Like

try printing script:GetFullName()

Animations should be done on the client when they are for the player.

Delete tool.Equipped & tool.unequipped and move them to the localscript.

Then define plr, chr, and hum like so:

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local hum = chr:FindFirstChildOfClass "Humanoid"
local mouse = plr:GetMouse()

local Tool = script.Parent.Parent

local remote = game.ReplicatedStorage:WaitForChild "ThrowChair"

--other vars

local function onCleanup()
 animationTrack:Stop()
  animationTrack2:Stop()
end

Tool.Equipped:Connect(function()
  animationTrack:Play()
  task.wait(0.1)

 animationTrack:Stop()
 animationTrack2:Play()
end)

Tool.Activated:Connect(function()
  remote:FireServer(mouse.Hit.Position)
end)

Tool.Unequipped:Connect(onCleanup)
Tool.Destroying:Connect(onCleanup)

And on the server:

local Handle = script.Parent
local Tool = Handle.Parent

local remote = game.ReplicatedStorage.ThrowChair

remote.OnServerEvent:Connect(function(plr, pos)
  --if typeof(pos) ~= "Vector3" then return end
  local clone = Handle:Clone()
  --other initialization
  clone.Parent = workspace

  --remove the stop calls since destroying handles those
Tool:Destroy()
end)

This will make it less confusing and animations will be replicated because you’re animating the player’s character.

Hope this helps!