#Can someone explain to me why this doesnt work

1 messages · Page 1 of 1 (latest)

karmic sonnet
#

im trying to retrieve data when a player dies so im suing chracteradded when they load back in but this some how just breaks my entire game ans leaves no errors

hushed trail
#

you need to check if the player loaded if not wait

#

player.CharacterAdded:Wait()
remotepassave:FireClient(player, x5booleansave)```
#

also you're connecting it multiple times

#

which causes repeated fires to the client and memory leaks (lag)

#

@karmic sonnet

#

i'm really confused on what you're tryna do here honestly.

#

I can help you rewrite the code but i need to know when and what for you're calling the server

karmic sonnet
# hushed trail I can help you rewrite the code but i need to know when and what for you're call...

so "x5" is a boolean sent from the client to the server indicating that the thing that what something equals true over on the client (which by default equals false) so it sends true over to the server so the server can save it and return it when the player leaves and joins back the game (which works fine) or when the player dies (which completely breaks everything to the point button.mouseclick1 doesnt work anymore and it should

#

yo i think i legit just solved why soemthing is broke explaing it to u

karmic sonnet
hushed trail
#

use remotefunctions

hushed trail
karmic sonnet
karmic sonnet
hushed trail
#

I see you're trying to return the same value as a confirmation the value

#

Remote event is like throwing a rock

#

remote function throwing a rock and they throw the same rock at you (in your case) or a different one

#

that can contain differen args

hushed trail
#

do playeradded then character added

#

under character added fire the client with the args as a remote function

#

so lua local coolevent = remotefunction:InvokeServer(bool)

#

this on the client

#

on the server

karmic sonnet
karmic sonnet
hushed trail
#
local cooleventonserver = --path to it

cooleventonserver.OnServerInvoke = function(player, bool)
    player.Character or player.CharacterAdded:Wait()

    return bool
end)
``` if you want to use remotefunc
#

really just start by using remote functions

#

the error you're getting is because you're setting the data too fast many times

hushed trail
karmic sonnet
tiny ingot
tiny ingot
# karmic sonnet so i did that and it worked but maybe i put it in the wrong spot tho or sum

fetch from datastore once, only one time, shouldn't need to fetch it again. store changes in a table, save periodically (once every ~5mins or so) and on playerremoving and game:bindtoclose. you're getting the throttling error because you're using datastores inappropriately like every time the player presses jump you call datastore:setasync that will get you throttled 👍

#

in fact if you want to work with datastores it may be easier for you to just use profilestore

#

it does all the difficult work like saving and loading and catching errors for you, all you have to care about is what data goes into the profile and reading it the first time, profilestore generally does the rest

karmic sonnet
hushed trail
karmic sonnet
#

but i am still getting the error even though its gone and I wasnt getting any befroe

hushed trail
#

what error

#

rate limiting?

hushed trail
#

can you send both client and server

karmic sonnet
#

also how do i make this work?

hushed trail
#

you are doing a cyclic firing

#

player joins > server fires client > client fires server > server fires client and goes on

tiny ingot
#

it doesnt look cyclic but it is creating infinite connections

hushed trail
#

oh nvm

#

i didnt see the conn

tiny ingot
#

but it might be cyclic because the remotes the variables are actually connected to aren't shown

hushed trail
#

first of all remove that connection

karmic sonnet
#

it doesnt cycle

tiny ingot
#

the infinite connections is bigger problem tho

hushed trail
#

charadded

#

its a huge memory leak

karmic sonnet
hushed trail
#

everytime the player respawns

#

its like +1

hushed trail
tiny ingot
karmic sonnet
hushed trail
#

Are you trying to restore the player purchase?

#

the 5x speed/

tiny ingot
hushed trail
#

im being 100% honest

karmic sonnet
#

yeah when they die the boolean that turned true after they bought it goes to false so i want it to be true again and it works but i dont want a memory leak

solid ospreyBOT
#

studio** You are now Level 13! **studio

hushed trail
#

youre better off restarting

#

otherwise connec characteradded only once

solid ospreyBOT
#

studio** You are now Level 6! **studio

karmic sonnet
tiny ingot
#

how many times does this code connect to player.CharacterAdded?

hushed trail
#

the same thing

#

but instead it just prints

#

spawned

tiny ingot
#

he is currently doing the same thing

hushed trail
#

yeah js to make him understand

tiny ingot
#

every time the remote gets fired, a new connection to player.characteradded is created

#

because there is no limit, this is technically a memory leak because over enough time, or enough deaths, or if a cheater deliberately fires it without having died, this can reach hundreds to thousands, infinitely many connections. and each connection takes up a small amount of memory. it never gets cleared until the player leaves so if the player stays long enough this could eventually crash a server - though it's unlikely because how many times the player has to fire the remote for it to cause a crash, it's still a memory leak.

karmic sonnet
#

so even though the characteradded isnt going off when the remote event is fired its creating a connection? (whats a connection?)

tiny ingot
#

the :Connect() is a connection

#

you could use :Once() but you're using it wrong in the first place

hushed trail
#

if you wanna keep it there do :Once()

karmic sonnet
#

so how do i move it somewhere else? and get rid of the connection?

hushed trail
#

When you are trying to restore the player purchase

tiny ingot
hushed trail
#

where are you storing that bool

hushed trail
#

in the client tho?

tiny ingot
hushed trail
karmic sonnet
tiny ingot
#

the sentence 'restore a purchase' makes 0 sense to me

hushed trail
#

player buys item

#

leaves

#

rejoins

#

still has item

tiny ingot
#

that's just normal save/load

#

nothing is being "restored"

hushed trail
#

you are restoring it when joining another instance

#

which can be considered save load

tiny ingot
#

and it doesnt have anything to do with characteradded

#

i'm asking to find out why characteradded and the remote is there

hushed trail
#

🤷

tiny ingot
#

why not just characteradded:connect( APPLY_PURCHASE(char) ) ??? is that what they're trying to do? like this is why i asked and you should let them answer

tiny ingot
#

just trust the process bruh

hushed trail
#

👍

tiny ingot
karmic sonnet
# tiny ingot well thats why i was asking what are you trying to do exactly

when a player dies, the boolean that gets turned to true when they buy a gamepass then gets turned to false and I want it to remain true so to do that when the player first loads into the game the datasave fires to the server the boolean it saved when the player bought the gamepass then when the client receives itr and it equals true, the client fires back to the server the same boolean so the part where the infinite connections is knows what boolean it is so then if a player dies characteradded can go off and return the boolean back to the client

hushed trail
#

oh so its a gamepass?

karmic sonnet
#

yeah

hushed trail
#

just see if the player owns it when rejoining

tiny ingot
tiny ingot
#

yeah that's it. marketplaceservice/players close enough hehe

hushed trail
#

also for some reason

#

this method

#

doesnt actually update

#

when the player buys it ingame

tiny ingot
#

yes also capture promptgamepasspurchasefinished

hushed trail
#

or unowns the gamepass

hushed trail
tiny ingot
hushed trail
#

and also use ownsgamepass to check when rejoining

tiny ingot
#

so ignore unowning it

hushed trail
#

i mean no one would disown a gamepass anyway

tiny ingot
#

that's the first step - capturing when the player owns the gamepass and when they do not

hushed trail
#

just a detail

karmic sonnet
hushed trail
karmic sonnet
#

what

hushed trail
#

youre making a ladder of if

#

which is just unoptimal

tiny ingot
hushed trail
#

make a table

tiny ingot
#

that's simple enough

tiny ingot
#

doing this is very simple - store the result of however you capture the gamepass ownership, then apply it on characteradded

#

you don't need any remotes to :fireserver to do this, the server should already know in advance whether the player owns the gamepass or not. do not trust the client to tell the server if they own the gamepass or not.

karmic sonnet
#

so i should userownsgamepassasync?

tiny ingot
# karmic sonnet so i should userownsgamepassasync?

yes, store a table that holds the results of userownsgamepass when the player first joins, then update this table when the promptgamepasspurchasefinished is fired - you also need to update the current character when this happens

#

then whenever the player's characteradded is called, you can just refer to this table.

hushed trail
#

instead of doing a ladder of if

#

store the ids in a table

#

and retrieve upon purchase finished

#

so instead of having 50 gamepasses and 50 ifs you have 4 lines

tiny ingot
#

the table would look something like ```lua
local playerGamepasses = {
[my_userid] = {
["x2Speed"]= marketplaceservice:UserOwnsGamepass(my_userid); -- etc, do this in playeradded
["x3Speed"]=false;
["x4Speed"]=true;
}
}

function applyGamepassSpeed(player)
local passes=playerGamepasses[player.UserId]
local speedbonus=1
if passes["x2Speed"] then speedbonus+=1 end
if passes["x3Speed"] then speedbonus+=2 end
...etc
-- remote:fireclient(player,speedbonus)
-- or player.character.humanoid.WalkSpeed=16*speedbonus, or whatever else
end```

#

then just call the apply function on the purchase event, and on characteradded

#

very simple 👍

hushed trail
#

more future proof

karmic sonnet
#

ignore local speedboost = 1

#

and the player part of the applyspeedpass after characteradded

tiny ingot
# karmic sonnet like this?
function applyspeedpass(player)
...
applyspeedpass()``` please test your code before asking ![Thumbs](https://cdn.discordapp.com/emojis/1283014349086134322.webp?size=128 "Thumbs") 
always know what's in your variables, never assume. debug everything.
#

but generally the shape of it looks fine, you don't need gamepasstable[player] if you're doing it like this since the gamepasstable is an upvalue and only exists for that one player

#

i'll say this, you're quite a long way away from s1 rank fingerguns

#

only edge case left i can see really is if the player's character gets added before the player is ready to receive remotes, but aside from that close enough i guess 🤷

karmic sonnet
#

thx for all ur guys help

tiny ingot
stark jay
#

@tiny ingot @karmic sonnet never use PromptGamepassPurchaseFinished for handling purchased products

#

use ProcessReceipt

tiny ingot
#

PromptGamepassPurchaseFinished does not fire for devproducts.

#

ProcessReceipt does not fire for Gamepasses

#

hope that clears up your misunderstanding, i know what you're talking about and that's only an issue for devproducts, not gamepasses.

tiny ingot
#

based on the research i did, people who already have a gamepass can still fire the purchasefinished multiple times but they have to actually own the gamepass first, so design accordingly - gamepasses should not be tied to consumable or temporary bonuses 👍