Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with health kit (remote events/functions)? [SOLVED]

Asked by 4 years ago
Edited 4 years ago

I've been working on a health kit the past 2 weeks (learning a lot) and I've come across two problems.

  1. The remote events still fire when the health kit is unequipped.
  2. I can't figure out a way for my server script to read a client side variable.

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)
0
Why does your local script have identical chunk of lines near the bottom? You mentioned that the remote events still fire when the heatlh kit is unequipped, but I don't see no unequip. To read the client side variable can't you just access it on the server through the PlayerGui? Xx_FROSTBITExX 116 — 4y
0
The identical lines is two seperate remote events for detecting whether the mouse is held down or released. I didn't think about trying to access it in the PlayerGui, i'll try it out now. sidewinder44 20 — 4y
0
Server doesn't recognize the health gui in player gui, so I'll try to figure out a way storing a value to be accessed by both scripts in the tool itself. sidewinder44 20 — 4y
0
That's unfortunate. Maybe you can utilise RemoteFunctions. Send the remote, return it back on the client, update the gui? Xx_FROSTBITExX 116 — 4y
0
Seems like that'd be exactly what I need, client to server then back to client again. Time to learn. sidewinder44 20 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

0
Wow, this is awesome man. The booleans in the first question is a genius solution, I will be doing that. The 2nd answer I know works, but I need to read carefully so I'll be doing that tomorrow. (its late). Thanks a lot man, I really appreciate the help. I'll let you know tomorrow how everything turns out. sidewinder44 20 — 4y
0
I want to accept your answer but I haven't used this website in ages, how would I accept? sidewinder44 20 — 4y
0
Well, first try my suggestions and then accept my answer if it works! The accept answer option will be located below my post. BestCreativeBoy 1395 — 4y
Ad

Answer this question