Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with this IntValue changing script?

Might have missed something out, or forgotten something, but I think it is all there. (Both scripts are normal scripts not local) Okay, so I have created a script in which I have a union (Called Apple, but that is irrelevant) , when you touch the apple the apple will be removed from the workspace and your apple value will be +1. I have put an IntValue in the Player, like so:

game.Players.PlayerAdded:connect(function(player)
	    player.CharacterAdded:wait() --Waits until the CharacterAdded event has fired.
	    wait(1)
	    local Apple_count=Instance.new("IntValue") -- New IntValue
	    Apple_count.Parent = player
	    Apple_count.Name = "Apple_count" --Give a name to the IntValue, so you can easily identify it in other script
		Apple_count.Value=0
	end)

Now for the bit that doesn't work, the apple part (This is in a part, in the Workspace)

script.Parent.Touched:connect(function(player)
	print (player.Parent.Name) --This would print the name of the person who touched the brick
	local Player_name = player.Parent.Name
	local Apple=script.Parent
	Apple:remove() -- Haven't tested the remove part, because the script wasn't working without it, anyway.
	game.Players.Player_name.Apple_count.Value=game.Players.Player_name.Apple_count.Value+1
	end)

The Output states that Player_name is not a valid member of Players. Thanks.

3 comments

0

I edited my answer to include a description of how to use FindFirstChild

0

Thanks.

0

It works.

2 answers

✓ Accepted answer

When you use .name, you're asking for that "name" in particular, the way you wrote it.

When you are asking for something with a particular value, use [ someName ]:

game.Players[Player_name].Apple.Value = ...

Illustrating why this works:

print( game.Players ) -- Players

-- same thing as:
print( game["Players"] ) -- Players

-- same thing:
local service = "Players"
print( game[service] ) -- Players

-- STILL same thing:
Workspace = "Players" -- (Don't do this in actual code!)
print( game[Workspace] ) -- Players

It's just usually more convenient to use the .: it also makes it clearer which names don't change (Players, Apple, Value) and which ones are variable(s) (Player_name)


Alternatively you could use the :FindFirstChild(Player_name) method, which in this case would have identical functionality.

In general, :FindFirstChild is useful because rather than throw an error upon there not being something of Player_name present, it will just return nil, which you (the programmer) can catch.

player = game.Players:FindFirstChild( Player_name )

if player then
	-- There **WAS** a player named `Player_name`
	player.Apple.Value = ...
else
	-- There WAS NOT a player
	-- DON'T do anything with `player` or there will be
	-- an error.
end

(The check is only necessary when you aren't sure that it exists.

If you are sure, it would make more sense to use []. You aren't sure if

  • The name was generated somehow from users or randomly

  • You got the name from an object, but it's been any time (wait() or any event) since you verified they were still there

  • Something in your script could have deleted the object

)

1 comment

0

Thanks, for the detailed answer, I have already accepted however, could you show me breifly how FindFirstChild works, in this(or any) instance or should I post it as a new question?

game.Players.Player_name is looking in Players for an actual player named "Player_name." If you want to index it based on that string, you should be using FindFirstChild.

game.Players:FindFirstChild(Player_name).Apple_count.Value

Alternatively, you could use brackets

game.Players[Player_name].Apple_count.Value

1 comment

0

I appreciate the fact you answered my question, consequently I have given you a upvote, however due more content I accepted the other answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.