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

how to change value through script?

Asked by 4 years ago
local part = script.Parent.Touch
local part2 = script.Parent.TouchAgain
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AddValue = ReplicatedStorage.Events:WaitForChild("AddValue")

part.Touched:Connect(function(Find)
    local Parent = Find.Parent
    local humanoid = Parent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then

    end
end)

everything works apart from the actual things I want it to do.

I am trying to make it so it adds

player.HP.Value = player.HP.Value + 1

however I am doing it in a script and not in a local script and that won't be possible, how can I make it work?

2 answers

Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

:GetPlayerFromCharacter allows you to get a player from their character model. Use the following code (inside the if humanoid check):

local player = game.Players:GetPlayerFromCharacter(Parent)

if player then
    AddValue:FireClient(player)
end

Hope this helped!

0
the check if humanoid code already works, I need help setting up the system that adds value to the value inside of the player, but I can't as its a script and not a local script. I can't access game.Players.LocalPlayer.HP.Value imerchanti 0 — 4y
0
AddValue:FireServer(number) works but only from LocalScript, how to do it from just a script? imerchanti 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You can still access HP.Player.Value from the server, or a script. GetPlayerFromCharacter returns the player associated with the character that touched the part, and from there it's a simple matter of defining the path to your value.

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(Find)
    local player = Players:GetPlayerFromCharacter(Find.Parent)
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        local HP = player:WaitForChild("HP")
         if HP then
        HP.Value = HP.Value + 1
          print(HP.Value)
    end
end)

Answer this question