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

Text not changing after setting values?

Asked by
OldBo5 30
6 years ago
Edited 6 years ago

Ok, So I self-debugged it and it does change the value, BUT not the text. It is getting a little annoying so I just came here to ask a question about it.

local plr = game.Players.LocalPlayer
local sp = Instance.new("IntValue")
sp.Value = 100
plr.PlayerGui.MainUI.Display.SprintPower.Text = "Sprint Power: " ..sp.Value
sp.Parent = plr
while true do
wait()
function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
local character = plr.Character
if character then
    local hum = character:WaitForChild"Humanoid"
    if hum then
        hum.WalkSpeed = 26
    end
else
print("no character")
end
            sp.Value = 100
            wait(1)
            sp.Value = 50
            wait(10)
            sp.Value = 0
            if sp.Value == 0 then
                plr.Character.Humanoid.WalkSpeed = 16
            end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
function onKeyPressEnd(inputObject, gameProcessedEvent)
local character = plr.Character
if character then
    local hum = character:WaitForChild"Humanoid"
    if hum then
        hum.WalkSpeed = 16
    end
else
print("no character")
end
            sp.Value = 50
            wait(1)
            sp.Value = 100
    end

game:GetService("UserInputService").InputEnded:connect(onKeyPressEnd)
end

If anyone knows the error please answer this! :)

0
First off I'd put the "Sprint Power" .. sp.Value in brackets, I don't know if this will change anything but it usually helps in telling the computer what to change the text to. Second, make sure you use a local script when calling on "Local Player", because it will substitute the player for nil unless it's in a local script. EnderGamer358 79 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Try this, this happened to me while making a gui for my place, I'm pretty sure it's because the script gets the sp value when it fires, and doesn't re-check when it changes, the solution was adding an extra function to detect when the value changes and update the text.

local plr = game.Players.LocalPlayer
local sp = Instance.new("IntValue")
sp.Value = 100
plr.PlayerGui.MainUI.Display.SprintPower.Text = "Sprint Power: " ..sp.Value
sp.Parent = plr

while true do
wait()
function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
local character = plr.Character
if character then
    local hum = character:WaitForChild"Humanoid"
    if hum then
        hum.WalkSpeed = 26
    end
else
print("no character")
end
            sp.Value = 100
            wait(1)
            sp.Value = 50
            wait(10)
            sp.Value = 0
            if sp.Value == 0 then
                plr.Character.Humanoid.WalkSpeed = 16
            end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
function onKeyPressEnd(inputObject, gameProcessedEvent)
local character = plr.Character
if character then
    local hum = character:WaitForChild"Humanoid"
    if hum then
        hum.WalkSpeed = 16
    end
else
print("no character")
end
            sp.Value = 50
            wait(1)
            sp.Value = 100
    end

game:GetService("UserInputService").InputEnded:connect(onKeyPressEnd)
end
sp.Changed:connect(function(NewValue)
plr.PlayerGui.MainUI.Display.SprintPower.Text = "Sprint Power: " ..sp.Value
end)


0
Thanks! OldBo5 30 — 6y
Ad

Answer this question