Client Server and events really break my brain. I am making a magic system where when you press Q, you cast a spell. I am working on the fireball (my first spell) and it works really well. However, I want to connect this to a mana system. I cast the spell, but it doesn't take away the mana. It takes 35 each cast and regenerates 10 every second. Yet, I can cast it only 4 times (35 goes into 100 4 times before it goes past it), so that could explain why it only happens 4 times. I checked my values in Explorer whilst running the game and still it stays at 100.
Here is the script that is in StarterCharacterScripts. It is a local script:
--// Services \\-- FIREBALL
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
--// Variables \\--
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local Remote = ReplicatedStorage.FireBallEvent
local Mouse = plr:GetMouse()
local amount = game.StarterGui.ManaBar.Bar.Valuee
local sound = game.StarterPlayer.StarterCharacterScripts.LocalScript.Sound
local Gui = plr.PlayerGui.ManaBar
local Fill = Gui.Bar.Fill
--// Settings \\--
local Debounce = true
local Key = 'Q'
local player = game.Players.LocalPlayer
local Animation = Instance.new("Animation")
Animation.AnimationId = 'rbxassetid://2624904738'
function updateBarAndText()
Fill:TweenSize(UDim2.new((Gui.Bar.Valuee.Value/Gui.Bar.MaxValue.Value),0,1,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, .1, true)
end
UserInputService.InputBegan:Connect(function(Input, IsTyping)
if amount.Value <= 0 then
--Nothing shall happen
else
if IsTyping then return end
local KeyPressed = Input.KeyCode
if KeyPressed == Enum.KeyCode[Key] and Debounce and Char then
Debounce = false
Remote:FireServer(Mouse.Hit)
local LoadAnimation = Char.Humanoid:LoadAnimation(Animation)
LoadAnimation:Play()
sound:Play()
amount.Value = amount.Value - 35
updateBarAndText()
wait(1)
Debounce = true
end
end
end)
Here is the local script in my Mana bar GUI:
local amount = script.Parent.Valuee
local number = script.Parent.Number
local belowvalue = amount.Parent.MaxValue.Value - 10
number.Text = "Mana: "..amount.Value
amount.Changed:Connect(function()
if amount.Value >= 100 then
number.Text = "Mana: 100"
end
end)
amount.Changed:Connect(function()
if amount.Value <= 0 then
number.Text = "No Mana"
else
number.Text = "Mans: "..amount.Value
end
end)
while true do
wait(1)
if amount.Value <= belowvalue then
amount.Value = amount.Value + 10
elseif amount.Value >= amount.Parent.MaxValue.Value then
amount.Value = amount.Value + 0
wait(2)
end
end
while true do
wait(1)
if amount.Value <= 0 then
amount.Value = 0
wait(5)
end
end
And the main fireball script in case you need it (Server script):
--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService('Debris')
--// Variables \\--
local Remote = ReplicatedStorage.FireBallEvent
local FireBall = ReplicatedStorage.FireBall
--// Settings \\--
local Damage = 35
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
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()
FireBallClone.Fire.Speed = NumberRange.new(8,8)
wait(1)
FireBallClone.Fire.Enabled = false
wait(1)
FireBallClone:Destroy()
wait(3)
Debounce = true
end
end)
end
end)
Any help is appreciated

3 comments
You can only change values via ServerScript, or it won't replicate to server and value won't change
Like the person said above, any changes made on the client isnt visible to the server, you ll need a remote event, to lower the mana
How can I put that remote event into a script? I am not too familiar with them