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:
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:
player is nil. I don’t know how you defined it, but if you used Players.LocalPlayer, it doesn’t work on the server
i didnt use players.localplayer on the server
In that screenshot, at the very top is what he’s talking about
Now seems like a very good time to elaborate on what you did use
How did you define player
in the server script?
I may be wrong, but I think you’re firing the remote event wrong, you’re meant to locate the player first.
I used this piece of code
(the script in is the players backpack because its in starter player
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
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!