Reward item based on bool value

		local hasHoppa

		local success, err = pcall(function()
			hasHoppa = hoppadata:GetAsync(player.UserId)
		end)

		if success then
			hoppa.Value = hasHoppa
		else
			hoppa.Value = false
		end
		if hoppa.Value == true then
			snaphoppa:Clone().Parent = player:WaitForChild("Backpack")

		end

		hoppadata:SetAsync(player.UserId, hoppa.Value)
		hoppa.Changed:connect(function()
			print("Saving data...")
			hoppadata:SetAsync(player.UserId, hoppa.Value)	
			print(player.UserId.."'s data has been saved!")	

I’m trying to get it to reward you “Snap Hoppa”
when you have the bool set to true.
theres a lot of script unseen, but it doesen’t even give you the item

Anyone know why?

2 Likes

Did you define SnapHoppa? Can you show me that line of code? Any errors too?

You have to define the tool you want to give to them. Example:

local ToolReward = game.Workspace.Snaphoppa

You would just change workspace to the place your tool is in. If you still have errors show me your output tab.

image

No errors either. 0

I’ve defined it. I know all the basics haha

Sometimes i make simple mistakes so this could be one of them, But i honestly have no clue why this doesen’t work

You can’t do that(I think). Do this:

local ServerStorage = game:GetService("ServerStorage")
local ToolReward = game.ServerStorage:FindFirstChild("SnapHoppa")

Might need some tweaking.

I’ve done this in another script though. I doubt it would fix it but i’ll give it a try

Didn’t seem to work unfortunately

Have you print debugged it and checked the value was actually true? I’d also recommend try playing the game rather than testing it in Studio as bugs do occur frequently with DataStores in testing.

I’ll try printing, But I do test in game.

Odd. You said no errors in the output correct? The only thing I see wrong(in the script you provided) was you incorrectly defining the tool. Try this:

game.ServerStorage.snaphoppa:clone().Parent = Player.Backpack

I’m going to test your script out later and see if I can get anything from that. I also suggest using print statements to find where your code stops.
Edit: In the script you provided player isn’t defined, maybe try defining player.

defined player in a function higher up

Well I can’t test your code out a ton as I’m also assuming you defined hoppa up in your code as well. I’m probably missing something but I don’t see anything wrong with your script.

Does it fire, and what fires it?

I don’t think it fires. Its just in the script. if the hoppa value is true you get it

If it’s not in a loop or something, maybe a function, then wouldn’t it just fire when the script gets to it when the game starts?

Right, thats what i want it to do

I think you should try using BoolValue.Changed

Why are you calling a pcall for the DataStore if you’re not checking if the data actually saved? (Yes I know you do if success then but that’s unnecessary as if it doesn’t you can’t do anything about it unless you are storing everyone’s data to variables until the request queue becomes available again. Just saying that it’s slowing down the server to be doing that and complicates things more than they need to be.)

In this case you’re not telling where the tool is at. You’re only telling where it needs to go. So your script is just copying the root folder of the tool and cloning it to the client, which practically does nothing. That is why you’re script is not returning an error because it’s doing what you’re telling it to do.

Also I’ll be your console right quick

Variable “hoppa” is not defined. 
Object “snaphoppa” does not exist or is not defined. 

Unless there is more to the script that you haven’t posted, that’s the errors that I’m seeing, if your script is not returning an error, it means that you have defined the variable incorrect above the posted code snippet.

Hopefully this helped you out!

Don’t do separate datastores for single items, it’s very inefficient as one datastore limit can store 4mb. Just store data in a table then if your key in a table for hoppa returns nil then you know that the player doesn’t have hoppa.