OnServerEvent The Value Wont Change Or Is Nil? why does this happen

  1. What do you want to achieve? Keep it simple and clear!

I want to make it so that the value of CurrentChargingValue changed to the value of the result of holding time.

  1. What is the issue? Include screenshots / videos if possible!

i was trying to make a football game with a holding feature, when the person stop holding it will send the Holding/Charging Time Value 0 - 1. the value was fired to the server, on server event the currentChargingValue that has the value of 0 is changed to be the ChargingValue Number, but when i tested i think it still is 0 it hasnt changed the ball Y Axis, or maybe its nil. so i put a print on the script, and it prints out my username? not the value, how do i fix this i think iam doing something wrong.

LocalScript


-- Services
local UserInputService = game:GetService("UserInputService")

-- Variables
local ActivationKey = Enum.UserInputType.MouseButton2 -- Change to key that you want

local HoldDuration = 1 -- In seconds
local ChargingValue = 0

local StartingTick = 0

local Holded = false

local Event = game.ReplicatedStorage:WaitForChild("Foot")

local ScreenGui = script.Parent
local HoldingBar = ScreenGui:WaitForChild("HoldingBar")
local LoadingBar = HoldingBar:WaitForChild("LoadingBar")

UserInputService.InputBegan:Connect(function(inputObject)
	if (inputObject.UserInputType == ActivationKey) then
		-- Reset Values
		ChargingValue = 0

		LoadingBar.Size = UDim2.new(0, 0, 1, 0)

		-- Update Starting Tick
		StartingTick = tick()

		-- Enable UI
		ScreenGui.Enabled = true

		-- Update Holded Value
		Holded = true
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if (inputObject.UserInputType == ActivationKey) and Holded then
		-- Update Holded Value
		Holded = false

		-- Disable UI
		ScreenGui.Enabled = false

		-- Firing The Server
		
		Event:FireServer(ChargingValue)
		print(ChargingValue)
	end
end)

-- Initialize
while true do
	if Holded then
		-- Normalized Value
		local alpha = ((tick() - StartingTick) / HoldDuration)

		-- Update Charging Value
		ChargingValue = math.clamp(alpha, 0, 1)

		-- Update 
		LoadingBar.Size = UDim2.new(ChargingValue, 0, 1, 0)
	end

	task.wait()
end

ServerScript


local ball = script.Parent
local CurrentChargingValue = 0
local event = game.ReplicatedStorage:WaitForChild("Foot")

event.OnServerEvent:Connect(function(ChargingValue)
	wait()
	
	CurrentChargingValue = ChargingValue
	
	print(CurrentChargingValue)
	
	ball.Touched:Connect(function(subject)
		if subject.Parent:FindFirstChild("Humanoid") then
			local hrp = subject.Parent:FindFirstChild("HumanoidRootPart")

			local forceDirection = (hrp.Position - ball.Position).unit
			forceDirection = Vector3.new(forceDirection.x, CurrentChargingValue, forceDirection.z)

			local forceMag = -50

			ball.Velocity = forceDirection * forceMag
			
		end
	end)
end)

Sorry if had a bad explanation on what i meant but its bassicly like changing the kick direction of the ball so that the ball goes up.

Change the function of onserver event to function(Player, ChargingValue)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.