Returning Zero Only

Hello Devs, I’m working on a Power Slapping game. I’m currently trying to deal damage to the humanoid using the speed of the mouse as its colliding with the character. Instead of returning a actual number it just returns 0. I’m completely confused on why its doing this. Please Help.

-- Services --
local PlrService = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- Constants / Stuff --
local Character = script.Parent
local LocalPlr = PlrService:GetPlayerFromCharacter(Character)
local LocalMouse = LocalPlr:GetMouse()
-- Constants / Remotes --
local GameRemotes = ReplicatedStorage:FindFirstChild('GameRems')
local MainEvent = GameRemotes:FindFirstChild('MainEvent')
-- Constants / Numbers --
local LastPosition = Vector2.new(0,0)
local MouseSpeed = 0
local CanSlap = true
----

-- Functions --
local function FollowPart()
	local Humanoid = Character:WaitForChild('Humanoid')
	local Head = Character:WaitForChild('Head')
	-- Check --
	local Part = Instance.new('Part')
	-- Setup Part --
	Part.Name = 'FollowPart'
	Part.Parent = Character
	Part.Size = Vector3.new(1,1,1)
	Part.Anchored = true
	Part.CanCollide = false
	Part.Position = Vector3.new(Head.Position.X,Head.Position.Y + 2,Head.CFrame.LookVector.Z + 10)
	-- Setup IKControls --
	Humanoid.ArmControl.Target = Part
	Humanoid.TorsoControl.Target = Part
	-- While Statement --
	while CanSlap do
		task.wait()
		Part.Position = Vector3.new(LocalMouse.Hit.X,Part.Position.Y,Part.Position.Z)
		-- Update Mouse --
		MouseSpeed = math.floor((Vector2.new(LocalMouse.X,LocalMouse.Y) - LastPosition).Magnitude/50)
		LastPosition = Vector2.new(LocalMouse.X,LocalMouse.Y)
	end
end
----

-- OnClientEvent --
MainEvent.OnClientEvent:Connect(function(SentEvent)
	-- Check --
	if SentEvent == 'Touched Part' then
		-- Update --
		MainEvent:FireServer('MouseSpeed',MouseSpeed)
	end
end)
----

-- Init --
FollowPart()
----
1 Like

can I see the .OnServerEvent?

3 Likes
-- OnServerEvent --
MainEvent.OnServerEvent:Connect(function(LocalPlr,SentEvent,MouseSpeed)
	-- Check --
	if SentEvent == 'MouseSpeed' then
		print(MouseSpeed)
		-- Deal Damage --
		
		script.Parent.Humanoid:TakeDamage(MouseSpeed)
	end
end)
----
2 Likes

When I did it earlier it worked perfectly fine. Scripted the exact same

2 Likes

could you print it on the client

1 Like

this is untrue, the terms global and local determine how that function can be accessed rather than what variables it can change.

1 Like

using local function outside every code block makes it act as a global function

1 Like

if that’s the case, then what do you think is the reason why it prints 0?

1 Like

well it’s certainly not about local and global functions since he stated that it worked before.

1 Like

Both the Server x Client will print the same Number. I changed it from local function to just function

1 Like

But if I toggle the comment on LastPosition = Vector2.new(LocalMouse.X,LocalMouse.Y) It returns a number higher than 0 but it gives me a horribly unrealistic number the mouse wasn’t even moving in.

2 Likes

@remcodesremcodes and @RatiusRat.

I Added 2 TextLabels. One that labels the current Mouse Speed and one that labels what the mouse speed was when hit. But I’ve realized that its not really accurate (or I don’t think it is). Here’s a clip I took of it.

1 Like

got one question, how do you calculate the damage? is it just the speed of the mouse when hit

(probably not because mobile will have some op powers)

1 Like

I calculate it from the Movement Speed Of The Mouse aka this line MouseSpeed = math.floor((Vector2.new(LocalMouse.X,LocalMouse.Y) - LastPosition).Magnitude/5)

I see that it’s still exceeding the MouseSpeed and not getting the accurate number from when it was hit.

i feel its calculating 0 because your math got it to 0 - 0.9999999999999999999999999999 and then your math.floor gets it to 0

could you try removing math.floor and see what it calculates

I do math.floor so it doesn’t return any decimals

yes but try removing it to see what it really calculates

1 Like

This is what I was referring to as not getting the number right off the bat.

It returns numbers like 0.9 0.8 0.7 and things of that nature