I am making a magic system in my RPG. The original script works very well on PC and I wanted to add mobile support. I got that bit sorted but on mobile I get the error in the title. I cannot find an alternative, I tried using UnitRay but that didn't work either. Any help is appreciated.
--// Services \\-- local ReplicatedStorage = game:GetService("ReplicatedStorage") local Debris = game:GetService('Debris') --// Variables \\-- local Remote = ReplicatedStorage.FireBallEvent local FireBall = ReplicatedStorage.FireBall local player = game.Workspace:FindFirstChild(game.Workspace.PlayerName.Value) --// Settings \\-- local Damage = 20 local ServerDebounces = {} Remote.OnServerEvent:Connect(function(plr, Mouse) if not ServerDebounces[plr] then ServerDebounces[plr] = true local Char = plr.Character or plr.CharacterAdded:Wait() local FireBallClone = FireBall:Clone() FireBallClone.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,-3) FireBallClone.Parent = workspace local BodyVelocity = Instance.new("BodyVelocity") BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) BodyVelocity.Velocity = Mouse.lookVector ----- This is the problem line BodyVelocity.Parent = FireBallClone spawn(function() for i = 0, 1000 do wait() FireBallClone.Size = FireBallClone.Size + Vector3.new(0.07,0.07,0.07) BodyVelocity.Velocity = Mouse.lookVector*i end end) ServerDebounces[plr] = false Debris:AddItem(FireBallClone, 10) local Debounce = true FireBallClone.Touched:Connect(function(h) if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Debounce then Debounce = false local Enemy = h.Parent.Humanoid Enemy:TakeDamage(Damage) FireBallClone.Transparency = 1 BodyVelocity:Destroy() wait(1) FireBallClone:Destroy() wait(3) Debounce = true end end) end end)
Edited with working code!
Local Script:
--// Services \\-- local ReplicatedStorage = game:GetService("ReplicatedStorage") --// Variables \\-- local Remote = ReplicatedStorage.FireBallEvent local PlayerScripts = script.Parent local Player = PlayerScripts.Parent local Mouse = Player:GetMouse() local Char = Player.Character or Player.CharacterAdded:Wait() local Debounce = false Mouse.Button1Down:Connect(function() local CharRP = Char.HumanoidRootPart if(Debounce == false) then Debounce = true Remote:FireServer((Mouse.Hit.Position - CharRP.Position).unit)<-- pass to the server the mockup MouseLookvetor Debounce = false end end)
Server Side:
--// Services \\-- local ReplicatedStorage = game:GetService("ReplicatedStorage") local Debris = game:GetService('Debris') --// Variables \\-- local Remote = ReplicatedStorage.FireBallEvent local FireBall = ReplicatedStorage.FireBall local player = game.Workspace:FindFirstChild(game.Workspace.PlayerName.Value) --// Settings \\-- local Damage = 20 local ServerDebounces = {} Remote.OnServerEvent:Connect(function(plr,MouseLV) if not ServerDebounces[plr] then ServerDebounces[plr] = true local Char = plr.Character or plr.CharacterAdded:Wait() local FireBallClone = FireBall:Clone() FireBallClone.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,-3) FireBallClone.Parent = workspace local BodyVelocity = Instance.new("BodyVelocity") BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) BodyVelocity.Velocity = MouseLV ----- This is the problem line BodyVelocity.Parent = FireBallClone spawn(function() for i = 0, 1000 do wait() FireBallClone.Size = FireBallClone.Size + Vector3.new(0.07,0.07,0.07) BodyVelocity.Velocity = MouseLV*i end end) ServerDebounces[plr] = false Debris:AddItem(FireBallClone, 10) local Debounce = true FireBallClone.Touched:Connect(function(h) if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Debounce then Debounce = false local Enemy = h.Parent.Humanoid Enemy:TakeDamage(Damage) FireBallClone.Transparency = 1 BodyVelocity:Destroy() wait(1) FireBallClone:Destroy() wait(3) Debounce = true end end) end end)
as you can see i've changed the code a little bit as passing the Mouse object to the server didnt work, BUT sending the mockup Lookvector does! thats what this example does. It incorporates the HumanoidRootPart.Position and the Mouse.Hit.Position from the local script so it keeps the server script happy. I haven't fully tested the code but it works as far as firing the fireball from your chest area and it slowly moves away then speeds up.
I think the problem you ran into is the ServerEvent object allways returns the player who fired it and the server cannot use the Mouse object from the clients/players. But we can make a workaround as i've done in the scripts above by passing the lookvector as a Vector3 Value.
Might need to refine when defining the players root part on the server side script by passing the character as a varable or something.
Hope this helps!
p.s looks like a cool idea you have going on here! :D
You’re trying to send the player’s mouse, an object that only exists on the client, to the server, meaning it will come up as nil.
In the LocalScript
sided, where you made the :FireServer(), please replace it to this:
local Mouse = game.Players.LocalPlayer:GetMouse() game.ReplicatedStorage.FireBallEvent:FireServer(Mouse.lookVector)
And then replace in line 27 of the server script to:
BodyVelocity.Velocity = Mouse
So you got the lookVector via localscript, and you won't get the lookVector in server script, because mouse is only seen from client.