Touch-based winpad connecting to data store

Hello Developers, today I was working on my game and just messing around trying to have fun improving on my skill of creating games. The game I’m working on is just for fun, although I would like some feedback if you would like to tell me, in the replies. First of all, If you want to play the game and try to tell me what I could improve on, that would be awesome, so I’ll leave a link to the game if you want to try it out. I’ll get to the scripting help in a second.

Let me introduce you to the game I’m working on if you want to play it or not.
Basically, you get put in a container empty on start, there are parts that spawn from the top of the container and you basically need to get to the top and touch the touch detector. When you touch the touch detector on the top, you should get 50 Cash, get reset and every brick will be cleared. as I said this was just for fun and testing.

https://www.roblox.com/games/6544633722/Part-Party

Let’s get to the scripting help.
This should be mentioned, I’m a new scripter and I am not experienced, although I know the basics of Lua. So, here is the problem I have. I basically need to make the touch detector give me 50 Cash when I touch it or someone else does, I made a pretty simple data store script and I’ve encountered some problems along the way of making the Touch detector script that gives you 50 coins. I’ve searched for help all around the internet and still couldn’t find the correct thing I’m looking for to solve.

So, this is the data store script I am using.

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore('MyDataStore')

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'

	local currency = Instance.new("IntValue", leaderstats)
	currency.Name = 'Cash' 
	currency.Value = 0

	local PlayerCashId = 'Cash_'..player.UserId
	local CashData

	local s, err = pcall(function()
		CashData = MyDataStore:GetAsync(PlayerCashId)
	end)

	if s then
		currency.Value  = CashData
	else
		warn(err)
	end

end)

game.Players.PlayerRemoving:Connect(function(player)

	local PlayerCashId = 'Cash_'..player.UserId

	local s, err = pcall(function()
		MyDataStore:SetAsync(PlayerCashId, player.leaderstats.Cash.Value)
	end)

	if s then
		print('Stored all Player Data')
	else
		warn(err)
	end
end)

You can see that I have an IntValue, A currency Called Cash in my position.
Now I want to make the Touch detector give 50 coins to the player that touches it. The problem is, that I have no idea how to make it do that. :sweat_smile:

So, This is the touch detector script I’ve made that clears the blocks and works perfectly fine, although I’m sure it could be better.

------------------------------------------------------------ Touch Money Giver (Work in progress)

----------------------------------------------------- Touch Detector Clear Children

wait(160)

local Detector = script.Parent
local SC = game.Workspace.SpawnedBricks.SpawnedCylinders
local SB = game.Workspace.SpawnedBricks.SpawnedBricks1
local SS = game.Workspace.SpawnedBricks.SpawnedSpheres
local SW = game.Workspace.SpawnedBricks.SpawnedWedges

local function Clear()
	SB:ClearAllChildren()
	SC:ClearAllChildren()
	SS:ClearAllChildren()
	SW:ClearAllChildren()
end

Detector.Touched:Connect(Clear)

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		Humanoid.Health = 0
	end
end)

------------------------------------------------------------ nil

As you can see that’s the Part Clearer script I’ve made. The wait(160) is to prevent exploiters from touching the touch detector, and abusing it, to annoy players that just want to have fun.

Now to the Touch detector part that gives the Cash, As I said, I don’t know how to make such a script so I would like to have some help with the replies.

Thanks for reading my post, This is the first time I’ve posted on the dev forum.

1 Like

I’m guessing the code you want to make the player earn cash is this?

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		Humanoid.Health = 0
	end
end)

You could use the hit.Parent as a parameter of game.Players:GetPlayerFromCharacter() to get the player from that humanoid, and if there was a player, jsut go to their leaderstats’ cash value and add 50 to it. You will need a debounce for this since it’ll spam the player with cash, but you could just check if their health is 0 and if it isn’t, damage them to 0 and give the cash

1 Like

Hello, This is not what I’m looking for.
I’m looking for the solution for the Cash value to be increased by 50 if a player touches the part that gives the coins, kills the player, and resets all the bricks in the container.

Although thanks for trying to help!

I was giving an insight as to how to do it since it’s better to give an explanation as to how to do it. The 2nd script I presume already kills the player and resets all the bricks and it just needs to give the player 50 cash?

1 Like

It resets the player character, clears the bricks out, and should give the player that touched the part 50 coins. I also made it so that there is a specific time to achieve the top and if you are under it you get kicked for Glitching/Exploiting.

What I mean by bricks is the bricks you climb inside the container not the whole map/the cash giver part.

Although thanks for the help!

What you could do for the cash Giver part is this

  • Set up a Touched event for that part and get the hit part
  • Use game.Players:GetPlayerFromCharacter(hit.Parent) to get the player if the character belongs to a player and store the result in a variable
  • Also make a variable for the humanoid of the Character
  • Was a player and humanoid found?
  • If yes, is the health of the Humanoid greater than 0?
  • If yes, get the Cash Value from the leaderstats and add 50 to it

I presume you’ll get the cash the same time as you reset?

1 Like

I presume you’ll get the cash at the same time as you reset?
Yes, that’s correcet.

The problem is that I’m a new scripter and don’t know how to do that.

Just another question, is this code

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		Humanoid.Health = 0
	end
end)

Used for the cash giver part or are these 2 separate parts? Asking so I can help with writing and explaining the example code I’ll provide

1 Like

It’s the same part that gives 50 Cash and resets the player, and clears the brick. Its not two seperate scripts or Parts, altough the datastore script is a different script.

And, the script is inside the part. So, its the first child of the TouchDetector part.
image
And yes, It’s in workspace.

Alright, here’s how you could do it

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent) --Gets the player
	if Player and Humanoid.Health ~= 0 then --Was the Player found and are they alive?
		Humanoid.Health = 0 --Kill the player
		Player.leaderstats.Cash.Value += 50 --Add 50 to their Cash
	end
end)

The order of operations is as follows

  • Get the Humanoid and the Player using FindFirstChild and GetPlayerFromCharacter respectively
  • Was a player found and is their health not 0?
  • Kill the player and add 50 to their health

This acts as a debounce as well so it only gets ran once. Please tell me if there’s any problem!

2 Likes

Alright, I’ll check it out ingame.

Perfect! It worked.
Thanks for the support and help.
Any feedback on the game?

2 Likes

There is one thing that is needed to make the script function perfectly. How do I make the countdown loop after the script gets executed? So that it’s not like you only wait once and you can abuse the script, again and again, I have to go now. Bye bye!

What do you mean by that? Which countdown loop? the wait(160)?

1 Like

Hello, I’m back home again. What I mean by wait(160) is to prevent hackers from touching the Touch Detector part so it restarts all bricks, and doesn’t have to wait 160 seconds after the next time they touch the part. This can be easily be avoided by looping the script over and over so that you have to wait 160 seconds every time you touch it. The reason it’s 160 seconds is that it’s impossible to reach the top within that time. That can prevent hackers from abusing it if the script is constantly looped instead of running once.

I think what you could do is Disable and Reenable the script, but that would still keep the connections. How does your round system work? Maybe with how you make the thing work I could figure out a way you could do it

I have no current round system. You can read my top post and check the script and potentionally see the wait 160 line.

I think I know how you could do it so players can’t touch it again until 160 seconds have passed, a table debounce. Basically we can make a table to contain if a player already touched the part and make them wait 160 seconds before we can we anything to the part again, and it’s simple to do as well!

local playerDebTable = {}

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent) --Gets the player
	if Player and Humanoid.Health ~= 0 and not playerDebTable[Player.Name] then --Was the Player found and are they alive and not in the table?
		playerDebTable[Player.Name] = true --Set their thing to true
		Humanoid.Health = 0 --Kill the player
		Player.leaderstats.Cash.Value += 50 --Add 50 to their Cash
		wait(160)
		playerDebTable[Player.Name] = false --Sets it back to false so the player can touch it again after 160 seconds
	end
end)

Same as before, but a few things are different

  • We made a table to use to contain the players who have already touched
  • we added a new condition to check if their thing in the table doesn’t exist or is false
  • We set their key in the table to true to prevent abuse, and then after 160 seconds, sets it back to false for usage again by that player
2 Likes

Let me try it. It should work if the code supports it, but I’m sure it does.