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

Why won't this twitter code give cash when it is correct?!

I made this code for twitter where if they type in the code 'code' they receive +10 Tokens. My script is not functioning right it doesn't give the player any Tokens. Now I am new to scripting, and I taught myself and I have recently taught myself properties like making a block transparent and cancollide on click, but I have trouble on these types of scripts! Please Help! I used a Local Script. The Output says: Players.Player1.PlayerGui.TwitterCodesGui.Submit.LocalScript:3: attempt to index local 'click' (a nil value) 14:27:07.734 - Stack Begin What is the problem in line 3? local stats = click:findFirstChild('leaderstats')

script.Parent.MouseButton1Click:connect(function(click)
	if script.Parent.Parent.Input.Text == 'Code' then
	local stats = click:findFirstChild('leaderstats') 
	if stats ~= nil then --If moneyholder exists then
			local Tokens = stats:findFirstChild("Tokens") --Get money
			Tokens.Value  = Tokens.Value + 10
		script.Parent.Parent.Input.Text = 'Me'
		script.Parent.Text = 'Success!'
		wait(1)
		script.Parent.Text = 'Submit'
	else
		script.Parent.Parent.Input.Text = ''
		script.Parent.Text = 'Invalid Code!'
		wait(1)
		script.Parent.Text = 'Submit'
	end
	end
end)

3 comments

0

The MouseButton1Click event doesn't pass any arguments to its listeners, so click is nil. Maybe replace it with script.Parent?

0

MouseButton1Click Doesn't return the function, so the click will do nothing

1 answer

✓ Accepted answer

The MouseButton1Click event doesn't return anything, so your 'click' parameter is nil.

You can index the LocalPlayer instead :)

local input = script.Parent.Parent:WaitForChild("Input")
local plr = game.Players.LocalPlayer --This is your player.
local code = "Code" --This is the current code.
local award = 10 --This is the current reward.
local db = false --This will prevent unnecessary firing of events.

script.Parent.MouseButton1Click:connect(function(click)
	if not db then --Now you can spam click ;D
		db = true
		if input.Text == code then
			-- findFirstChild is deprecated :)
			local stats = plr:FindFirstChild('leaderstats') 
			if stats then
				local tokens = stats:FindFirstChild("Tokens")
				tokens.Value  = tokens.Value + award
				input.Text = "Me"
				script.Parent.Text = "Success!"
				wait(1)
				script.Parent.Text = "Submit"
			else
				input.Text = ""
				script.Parent.Text = "Invalid Code!"
				wait(1)
				script.Parent.Text = "Submit"
			end
		end
		db = false
	end
end)

Note: You may consider using DataStores to check if the player has used the given code already.

5 comments

0

how do I make the code only work once for that specific player?

0

You could start by reading the answer all the way through <3

0

I am new to this how do I use data store I am deeply sorry.

0
0

Datastores can only be accessed from server scripts, though. So you will have to setup a RemoteEvent. There's an article explaining usage here: http://wiki.roblox.com/index.php?title=RemoteFunction_and_RemoteEvent_Tutorial

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