Hello there! I’m trying to make a script that gets the mouse.Target when the player chats a certain work, but for some reason its saying that I’m indexing nil with mouse.Target.
How can I get this script to work? all help is appreciated
here’s the script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounce = false
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
Player.Chatted:Connect(function(msg)
if msg == "Immobilus" and not debounce then
print("working")
local witch = Character:FindFirstChild("Witch")
if witch.Value == true then
print("is a witch")
local magic = Character:FindFirstChild("Magic")
if magic.Value > 60 then
print("enough magic")
debounce = true
magic.Value -= 60
local mouse = Player:GetMouse()
local target = mouse.Target
print(mouse.Target)
end
print("debounce")
wait(5)
debounce = false
print("can work now")
end
end
end)
end)
end)
The mouse is a client side thing, simple fact is that you can’t access it from the server. You’ll have to use userinputservice and a remote event (or remote function) to get stuff from the mouse on the client to the server.
Local script in starter character scripts and a remote event in replicated storage
Get the player’s mouse in the local script and pass it to the server as a parameter.
Receive it on the server. You now have the player’s mouse.
You’ll have to change the server script a bit:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounce = false
ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, mouse)
local Char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
Player.Chatted:Connect(function(msg)
if msg == "Immobilus" and not debounce then
print("working")
local witch = Character:FindFirstChild("Witch")
if witch.Value == true then
print("is a witch")
local magic = Character:FindFirstChild("Magic")
if magic.Value > 60 then
print("enough magic")
debounce = true
magic.Value -= 60
local target = mouse.Target
print(mouse.Target)
end
print("debounce")
wait(5)
debounce = false
print("can work now")
end
end
end)
end)