Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to call a RBXScriptConnection value? How do I fix this error? (Writing a gamepass script)

Asked by 1 year ago

So I decided to make some scripts where when I touch a part, it prompts me a gamepass that gives me extra walkspeed when I buy it. However, when I was done writing the code (I used a remote function to detect the player touching the part, checking if the player has the gamepass, and giving the player the perks) , it gives me the following error message:

> 20:01:32.296 attempt to call a RBXScriptConnection value - Client - GamepassHandler:26

Here are my scripts:

DetectGamepassRequest (Located in the part):

> local part = game.Workspace.Part
> local MarketplaceService = game:GetService("MarketplaceService")
> local gamepassId = 74392253
> local RS = game:GetService("ReplicatedStorage")
> local GamepassFunction = RS.GamepassFunction
> 
> part.Touched:Connect(function(otherPart)
>   local humanoidCheck = otherPart.Parent:FindFirstChild("Humanoid")
>   local player = game.Players:FindFirstChild(otherPart.Parent.Name)
>   if humanoidCheck then
>       GamepassFunction:InvokeClient(player)
>       if not player then
>           return
>       end
>   end
> end

GamepassHandler (Located in StarterPlayerScripts):

> local part = game.Workspace.Part
> local MarketplaceService = game:GetService("MarketplaceService")
> local gamepassId = 74392253
> local RS = game:GetService("ReplicatedStorage")
> local GamepassFunction = RS.GamepassFunction
> local player = game.Players.LocalPlayer
> 
> local function promptGamepass()
>   local playerUserID = player.UserId
>   local hasPass = false
>   
>   local success, errorMessage = pcall(function()
>       hasPass = MarketplaceService:UserOwnsGamePassAsync(playerUserID, gamepassId)
>   end)
>   
>   if not success then
>       warn("Error while checking if "..player.Name.."owns the extra walkspeed gamepass. Error info: "..errorMessage)
>       return
>   end
>   
>   if hasPass then
>       print(player.Name.." already has the walkspeed gamepass!")
>       return
>   else
>       MarketplaceService:PromptGamePassPurchase(player, gamepassId)
>       GamepassFunction:InvokeServer(player)
>   end
> end
> 
> GamepassFunction.OnClientInvoke = promptGamepass()
> 

GamepassPerksHandler (Located in StarterPlayerScripts):

> local part = game.Workspace.Part
> local MarketplaceService = game:GetService("MarketplaceService")
> local gamepassId = 74392253
> local RS = game:GetService("ReplicatedStorage")
> local GamepassFunction = RS.GamepassFunction
> local Players = game:GetService("Players")
> 
> local function giveGamepassPerks(player, gamePassId, wasPurchased)
>   if gamePassId == gamepassId and wasPurchased then
>       local char = player.Character
>       char.Humanoid.WalkSpeed = 32
>   end
> end
> 
> local function onJoin(player)
>   local UserID = player.UserId
>   local hasPass = false
>   
>   local success, errorMessage = pcall(function()
>       hasPass = MarketplaceService:UserOwnsGamePassAsync(UserID, gamepassId)
>   end)
>   
>   if not success then
>       warn("Error while checking if "..player.Name.."owns the gamepass! Error info: "..errorMessage)
>       return
>   end
>   
>   if hasPass then
>       local char = game.Workspace:FindFirstChild(player.Name)
>       char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed*2
>   end
> end
> 
> GamepassFunction.OnServerInvoke = MarketplaceService.PromptGamePassPurchaseFinished:Connect(giveGamepassPerks)
> Players.PlayerAdded:Connect(onJoin)

If possible, I also don't understand what a RBXScriptConnection is, so if anyone clarifies that would be great.

As always, thank you for your help and I appreciate taking your time out of your day to read this.

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

Use RemoveEvents instead since you don't need to return something from it. Move GamepassPerksHandler to ServerScriptService and make sure it's a normal script.

DetectGamepassRequest

local part = script.Parent
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local gamepassId = 74392253
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GamepassEvent = ReplicatedStorage:WaitForChild("GamepassEvent")

part.Touched:Connect(function(otherPart)
    local humanoidCheck = otherPart.Parent:FindFirstChildOfClass("Humanoid")
    local player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if humanoidCheck and player then
        GamepassEvent:FireClient(player)
    end
end)

GamepassEvent.OnServerEvent:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local success, hasPass

    repeat
        success, hasPass = pcall(MarketplaceService.UserOwnsGamePassAsync, player.UserId, gamepassId)
    until success

    if hasPass then -- to check if the player is not hacking
        character:WaitForChild("Humanoid").WalkSpeed = 32
    else
        player:Kick("Please stop exploiting the 2x speed gamepass! Reason: Hacking") -- anti cheat (optional)
    end
end)

GamepassHandler

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local gamepassId = 74392253
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GamepassEvent = ReplicatedStorage:WaitForChild("GamepassEvent")
local player = Players.LocalPlayer

GamepassEvent.OnClientEvent:Connect(function()
    local playerUserID = player.UserId
    local success, hasPass

    repeat
        success, hasPass = pcall(MarketplaceService.UserOwnsGamePassAsync, playerUserID, gamepassId)
    until success

    if hasPass then
        print("Player already owns the gamepass!")
    else
        MarketplaceService:PromptGamePassPurchase(player, gamepassId)
    end
end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(playerWhoPurchased, soldGamePassId, wasPurchaseSuccessful)
    if soldGamePassId == gamepassId and wasPurchaseSuccessful then
        GamepassEvent:FireServer()
    end
end)

GamepassPerksHandler

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local walkSpeedId = 74392253

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local success, hasPass

        repeat
            success, hasPass = pcall(MarketplaceService.UserOwnsGamePassAsync, player.UserId, walkSpeedId)
        until success

        if hasPass then
            character:WaitForChild("Humanoid").WalkSpeed = 32
        end
    end)
end)
0
The scripts you provided caused a script timeout exhaustion. When I added some wait() functions, it no longer lagged, but when I touched the part nothing worked. Any suggestions? Bloxyses 57 — 1y
Ad

Answer this question