generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: NOW another question

Previous Thread :: Next Thread 
Drybones3363 is not online. Drybones3363
Joined: 16 Aug 2013
Total Posts: 1547
02 Nov 2013 11:34 PM
anyone know a script for if you hit something, you get cash?
this is what i have so far
function onTouched(hit)
if (player ~= nil) then
player.leaderstats.cash.Value = cash.value + 15
end
end
script.Parent.Touched:connect(onTouched)
Report Abuse
Drybones3363 is not online. Drybones3363
Joined: 16 Aug 2013
Total Posts: 1547
02 Nov 2013 11:34 PM
last question by the way
Report Abuse
TheScriptAdventurer is not online. TheScriptAdventurer
Joined: 14 Oct 2013
Total Posts: 137
02 Nov 2013 11:37 PM
What do you mean by cash?
Do you mean a little in-game currency or robux/tickets (in the latter case, it is not possible).
Report Abuse
Drybones3363 is not online. Drybones3363
Joined: 16 Aug 2013
Total Posts: 1547
02 Nov 2013 11:38 PM
in-game currency
Report Abuse
TheScriptAdventurer is not online. TheScriptAdventurer
Joined: 14 Oct 2013
Total Posts: 137
02 Nov 2013 11:49 PM
Ok, you are definitely on the right track, but there is still quite a few problems with your code. One of the problems is that the way the game engine works, your touched event is gonna be triggered multiple times a second. So if some person touches the brick, they are gonna end up with a hundred of the in-game currency within probably just a second.

This is where debounce comes in. The idea behind it is that you have a bool variable that you have initially set to true. You would give the money if it is true, then set it to false, and then wait a few seconds, and set it back to true. This makes it so that your cash-awarding code is only triggered every once in a while. If having a global variable for each event is too troublesome, you can even make a function that returns a debounced version of your function.

function DebounceIt(func, t)
local debouncevar = true
return function (...)
if debouncevar then
func(...)
debouncevar = false
wait(t)
debouncevar = true
end
end
end

Now, your second problem is that player doesn't exist yet. You need to define it first. The way you would usually do it is check if the part that is hit is a child of a model with a humanoid object in it(almost always called Humanoid), and then use that model's name and use it to look up the player's object in the Players service.

function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
local player = game.Players:FindFirstChild(humanoid.Parent.Name)
if (player ~= nil) and (player:IsA("Player")) then
player.leaderstats.cash.Value = cash.value + 15
end
end
script.Parent.Touched:connect(DebounceIt(onTouched, 4)) -- uses the debounce function from before, and makes it so that the cash rewarding code is used only every four seconds or so.

Now, you also have to make sure you have the cash object in the leaderstats beforehand. And have the script you had be in the brick you want to be touched. At that point you are set!



Report Abuse
Drybones3363 is not online. Drybones3363
Joined: 16 Aug 2013
Total Posts: 1547
02 Nov 2013 11:57 PM
can you elaborate a little more. im a little confused
Report Abuse
TheScriptAdventurer is not online. TheScriptAdventurer
Joined: 14 Oct 2013
Total Posts: 137
03 Nov 2013 07:56 AM
I hate to be mean, but I can't exactly say it in lighter prose, but I will try. If you are just looking for a quick fix and you don't care if you don't understand it, here is the final code (put it in the part that you want to touch):

function DebounceIt(func, t)
local debouncevar = true
return function (...)
if debouncevar then
func(...)
debouncevar = false
wait(t)
debouncevar = true
end
end
end


function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
local player = game.Players:FindFirstChild(humanoid.Parent.Name)
if (player ~= nil) and (player:IsA("Player")) then
local stat = player.leaderstats:FindFirstChild("cash")
if stat then
stat.Value = stat.value + 15
end
end
end

script.Parent.Touched:connect(DebounceIt(onTouched, 4))








Now, I you want the easier explanation, here it is. Ok, the way the game works, the onTouched function will be called several times a second. DebounceIt uses a bool variable to make it so your event code is only triggered once every few seconds or so (about four in this case). We use the brick-that-we-have-touched's parent's Humanoid object to see if it is a player (or NBC). We then go ahead and use brick-that-we-have-touched's parent's name to find the player object in the game.Players service, confirm it is a player, and then add 15 bucks to the cash stat (if it exists. Note that if leaderstats doesn't exist, the script crashes).
Report Abuse
Drybones3363 is not online. Drybones3363
Joined: 16 Aug 2013
Total Posts: 1547
03 Nov 2013 08:03 AM
i used another way that works
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image