[Solved] MyProblem2

You can write your topic however you want, but you need to answer these questions:
Hello, my mob drop script works, but I wanted to add a feature that gives a player double money if they have the require gamepass. I need help and want to know why it won’t work.

  1. What do you want to achieve? Keep it simple and clear!
    All pass owners gets double the cash everytime they kill mobs.
  2. What is the issue? Include screenshots / videos if possible!
    The problem is that it does not give me double cash as I own the pass and no errors display in output.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried changing code and re-arranging the sequence but nothing works.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local GS = game:GetService("MPS")
local X2C = "234243242"
local PlayerData = game:GetService("SS"):WaitForChild("PF")

local MR = {
	

MR.GR = function(Hum,C,E)
	local DmgT = Hum:FindFirstChild("DmgT")
	if DmgT and DmgT.Value ~= nil then
		local XPlaya = DmgT.Value
		if XPlaya and PlayerData:FindFirstChild(XPlaya.Name) then
			PlayerData[XPlaya.Name].Data.Cash.Value = PlayerData[XPlaya.Name].Data.Cash.Value + Cash
			PlayerData[XPlaya.Name].Data.Experience.Value = PlayerData[XPlaya.Name].Data.Experience.Value + Experience
		elseif XPlaya and PlayerData:FindFirstChild(XPlaya.Name) and GS:PlayerOwnsAsset(game.Players.LocalPlayer.Name, X2C) then
			PlayerData[XPlaya.Name].Data.C.Val = PlayerData[XPlaya.Name].Data.Cash.Val + C*2
			PlayerData[XPlaya.Name].Data.E.Val = PD[XPlaya.Name].Data.Experience.Val + E
		elseif player then
			warn("NO STATS RN.")
		else
				warn("WHO????.")
		end
	end
end
2 Likes

Are you using a client-sided script to modify player stats? Tsk Tsk Tsk.

Anywhoo, the reason why your script errors out is because you’re passing in the player’s name instead of the player’s instance when calling MarketplaceService:PlayerOwnsAsset(). Just do:

GameShop:PlayerOwnsAsset(game.Players.LocalPlayer, X2Cash) instead

2 Likes

no I do not believe so, I am using a script inside of a mob to call the modulescript. It doesn’t display any errors with .Name and when I remove it, still the same outcome.

ModuleScripts will become either local/server based on the script that requires it.

If the script is required by a server script, the ModuleScript will have a server scope, therefore you can not call game.Players.LocalPlayer

If the script is required by a local script, the ModuleScript will have a client scope, therefore you can call game.Players.LocalPlayer.

You have to be mindful of this when using ModuleScripts. Also if you’re looking to use this function in other scripts, the better way of creating it is:

function MobReward:GiveReward(Hum,Cash,Experience)
	local DmgT = Hum:FindFirstChild("DmgT")
	if DmgT and DmgT.Value ~= nil then
		local XPlaya = DmgT.Value
		if XPlaya and PlayerData:FindFirstChild(XPlaya.Name) then
			PlayerData[XPlaya.Name].Data.Cash.Value = PlayerData[XPlaya.Name].Data.Cash.Value + Cash
			PlayerData[XPlaya.Name].Data.Experience.Value = PlayerData[XPlaya.Name].Data.Experience.Value + Experience
		elseif XPlaya and PlayerData:FindFirstChild(XPlaya.Name) and GameShop:PlayerOwnsAsset(game.Players.LocalPlayer.Name, X2Cash) then --This is the "ERROR"
			PlayerData[XPlaya.Name].Data.Cash.Value = PlayerData[XPlaya.Name].Data.Cash.Value + Cash*2
			PlayerData[XPlaya.Name].Data.Experience.Value = PlayerData[XPlaya.Name].Data.Experience.Value + exp
		elseif player then
			warn("NO STATS RN.")
		else
				warn("WHO????.")
		end
	end
end

Then you would simply do:

local MobReward = require(MobReward) -- \\ Replace with the path of the script

-- \\ whatever code before function call
MobReward:GiveReward(Hum,Cash,Exp) --\\ replace params with their respective data.

Thank you very much for the insight and pointing out that I cannot use local to call players from server side scripts. This really helped me know more, but changing the function structure is still the same as nothing happens. How would I go to call/define or find the player who owns the gamepass server-sided? Thanks in advance.


Documentation says PlayerOwnsAsset should not be used for passes, try using UserOwnsGamePassAsync.

1 Like

thank you for this, but how would I identify the user from serverside since I cannot use localplayer?

In the script that controls the mob, you could create a function to detect the player that kills the mob. Then pass it to GiveReward

This section of the code is the problem
you are first checking if the player exists then if it exists you give it same cash, so it dosent matter wether he has the gamepass or not, if he exists he will get the same cash from the first if condition,
Here is the fixed section of the code

if XPlaya and PlayerData:FindFirstChild(XPlaya.Name) then
		if GameShop:PlayerOwnsAsset(game.Players.LocalPlayer.Name, X2Cash) then
			PlayerData[XPlaya.Name].Data.Cash.Value = PlayerData[XPlaya.Name].Data.Cash.Value + Cash*2
        else
			PlayerData[XPlaya.Name].Data.Cash.Value = PlayerData[XPlaya.Name].Data.Cash.Value + Cash
        end
 
        PlayerData[XPlaya.Name].Data.Experience.Value = PlayerData[XPlaya.Name].Data.Experience.Value + Experience
        PlayerData[XPlaya.Name].Data.Experience.Value = PlayerData[XPlaya.Name].Data.Experience.Value + exp  --both exp and Experience?

elseif player then
	    warn("NO STATS RN.")
else
		warn("WHO????.")
end

edit: cleaned the code

This is great, but I ran into an error code for ref. the player

ServerScriptService.MobReward:31: attempt to index nil with 'UserId'

problem is because of this:

if GameShop:UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, X2Cash) then

How would I ref the player UserId as this is a server-side script?
(My bad I am using UserOwnGamepassAsync instead cause someone said it is more accurate)

if GameShop:UserOwnsGamePassAsync(XPlaya.UserId, X2Cash) then

1 Like

thank you for the help with restructuring my code. I am such an idiot I forgot that I already refer the player hahaha

(shoutout for the others too for setting me on the right path and trying to help)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.