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

Display an IntValue with a GUI when the Player touches a Part?

Asked by 10 years ago

How can I make it so when you hit a Block with an IntValue a GUI pops up with the Value of the IntValue? By the way, the IntValue changes its value every time you press it.

This is what I have that doesn't work:

script.Parent.Handle.Touched:connect(function(hit)
hit.IntValue.Value.Changed:connect(function(value)
script.Parent.Parent.Parent.PlayerGui.ScreenGui.TextBox.Text = value
end)
end)
0
Next time try to have your title match what your asking, "GUI and IntValue" doesn't help us know what your asking. User#2 0 — 10y

2 answers

Log in to vote
0
Answered by
User#2 0
10 years ago

It looks like you're using a tool, so I'll assume you are. I found three problems with this code.

First being that when your Handle is touched, anything can be touching it, not just the parts with IntValues. So you should check if hit has an IntValue (named "IntValue") inside of it. We can use FindFirstChild for this!

if hit:FindFirstChild("IntValue") then
    -- Stuff
end

Next, you're trying to use the Changed event incorrectly. The event can only be used on objects that you can view in the Explorer, like Workspace or a Part. Also, the argument given in changed is the name of the property that was changed. We can just set the gui's text value to the IntValue's Value though.

hit.IntValue.Changed:connect(function()
    -- Change the GUI
end)

And finally, when a player equips a tool it is moved to their Character. You seem to be trying to get the player using Parents, which doesn't work. Instead you can use the GetPlayerFromCharacter method on the tool's parent.

game.Players:GetPlayerFromCharacter(script.Parent.Parent)

Then just do what you were doing before (mostly), set the text of your GUI.

game.Players:GetPlayerFromCharacter(script.Parent.Parent).PlayerGui.ScreenGui.TextBox.Text = hit.IntValue.Value
Ad
Log in to vote
0
Answered by 10 years ago

This should work fine for you.

script.Parent.Handle.Touched:connect(function(hit)
hit.IntValue.Changed:connect(function(value)
game:GetService("Players")[script.Parent.Parent.Name].playerGui.ScreenGui.Text = hit.IntValue.Value
end)
end)

Answer this question