I've been working on a health kit the past 2 weeks (learning a lot) and I've come across two problems.
On the 2nd issue, I created a gui to display a percentage when the player heals, but I can't figure out a way to make the server side script read it and change it during the while true do loop. Once it reaches 100%, the player will be fully healed. (Player would be holding down their mouse)
--[Main script]--
local RemoteEvent1 = script.Parent:WaitForChild("IsMouseHeldDown") local RemoteEvent2 = script.Parent:WaitForChild("IsMouseReleased") local RemoteEvent3 = script.Parent:WaitForChild("HealingCharge") local MouseReleased = false RemoteEvent2.OnServerEvent:connect(function(player) MouseReleased = true --if mouse is released, set it to true end) RemoteEvent1.OnServerEvent:connect(function(player) if MouseReleased == true then --if mouse isn't held down, set it back to false. MouseReleased = false end local hum = player.Character:FindFirstChild("Humanoid") while true do wait(0.1) --(somehow access the player's local script GUI percentage)---- if MouseReleased == true then --if mouse is released, break the loop. break end end end)
--[Local script]--
local player = game.Players.LocalPlayer local Charge = 0 --[this is the percentage for the healing charge] local healthGui = Instance.new("ScreenGui") local percentage = Instance.new("TextLabel") local text = Instance.new("TextLabel") percentage.Size = UDim2.new(0,200,0,25) percentage.Position = UDim2.new(0.438,0,0.525,0) percentage.Text = Charge percentage.TextSize = 25 percentage.Font = "SciFi" percentage.TextColor3 = Color3.new(1, 1, 1) percentage.BackgroundTransparency = 1 text.Size = UDim2.new(0,200,0,25) text.Position = UDim2.new(0.438,0,0.577,0) text.Text = "Healed" text.TextSize = 25 text.Font = "SciFi" text.TextColor3 = Color3.new(1, 1, 1) text.BackgroundTransparency = 1 --healthGui.Enabled = false percentage.Parent = healthGui text.Parent = healthGui healthGui.Parent = player.PlayerGui local UserInputService = game:GetService("UserInputService") local RemoteEvent1 = script.Parent:WaitForChild("IsMouseHeldDown") local RemoteEvent2 = script.Parent:WaitForChild("IsMouseReleased") local RemoteEvent3 = script.Parent:WaitForChild("HealingCharge") local tool = script.Parent tool.Equipped:connect(function() UserInputService.InputBegan:connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then RemoteEvent1:FireServer() end end) end) tool.Equipped:connect(function() UserInputService.InputEnded:connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then RemoteEvent2:FireServer() end end) end)
Well, now I am going to answer your questions step-by-step.
Q. The remote events still fire when the health kit is unequipped.
Possible solution : Well, you can create a boolean and set its value to true or false, on the basis, whether the tool is Equipped
or Unequipped
. E.G.:
local check = false local tool = script.Parent tool.Equipped:Connect(function() if not check then check = true if check then -- Your code -- In your code, you can add like : if input.UserInputType == Enum.UserInputType.MouseButton1 and check then [It will fire only when check is true, i.e., when tool is Equipped] end end end) tool.Unequipped:Connect(function() if check then check = false end end)
Q. I can't figure out a way for my server script to read a client side variable.
Possible Sol : Well, the easiest way is to use PlayerGui
. E.G.:
-- In your code RemoteEvent1.OnServerEvent:Connect(function(player) -- Capital 'C' local PlayerGui = player:WaitForChild("PlayerGui") local HealthGui = PlayerGui:WaitForChild("Screen_Gui_Name").Health -- Just an example end)
Also, I saw in comments, that you have tried using PlayerGui
, if the above PlayerGui
step doesn't work. Here's an alternative method:
For the alt. method, we will be using RemoteFunctions
. Here's an example :
-- Local Script --// Variables local HealthGui = script.Parent -- Assumed as TextLabel local ReplicatedStorage = game:GetService("ReplicatedStorage") local RF = ReplicatedStorage.RemoteFunction -- Example local player = game.Players.LocalPlayer --// Functions local function HealthUI() -- Creating a custom function if HealthGui.Text == "100%" then -- If the Text becomes 100% local response = RF:InvokeServer(player.Name, HealthGui.Text) -- Sending message to server for successful 100% [player.Name for verification] if response then -- If the server returns true HealthGui.Text = "Healed!" end end end --// Main HealthGui:GetPropertyChangedSignal("Text"):Connect(HealthUI) -- Server Script --// Variables local ReplicatedStorage = game:GetService("ReplicatedStorage") local RF = ReplicatedStorage.RemoteFunction --// Main function RF.OnServerInvoke:Connect(player, check, Text) -- The player is the one who invoked the server --// Variables (again) local char = player.Character or player.CharacterAdded:Wait() -- Waits for player's character to load in local hum = char:WaitForChild("Humanoid") --// Main (again) if player.Name == check then -- If it's the player who actually invoked if Text == "100%" then -- Double checking the Text hum.Health = hum.MaxHealth -- Fully heals the player to its MaxHealth return true -- Returns true to the client end end end
Lemme know if it helps!