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

Why does my form not work despite having functional names and code?

Asked by 4 years ago
Edited 4 years ago

EDIT: It returned a error this time:

22:12:31.452 - ServerScriptService.FormServerside.Adrenaline:5: attempt to index number > with 'Character' 22:12:31.453 - Stack Begin 22:12:31.454 - Script 'ServerScriptService.FormServerside.Adrenaline', Line 5

I am working on an RPG game with my development team, and being the only scripter I have to write all of our code. This is a very rushed Form for the Race Human.

The race operates by having a set boost value and a keypress to launch an event to create an aura. I am going to add the boost code later, but this code simply does not work.

This is the code for the LocalScript in StarterPack:

repeat wait() until game.Loaded
local mouse = game.Players.LocalPlayer:GetMouse()
local Race = script.Race.Value
local Boost = script.Boost.Value
local Player = game.Players.LocalPlayer

function onKeyDown(key)
    if (key~=nil) then
        key = key:lower()
        if (key=="f")  then
            if Player.Backpack.ServerTraits.Race.Value == script.Race.Value then
                game.ReplicatedStorage.FormEvents.FormEvent:FireServer(Race, Boost, Player)
            else
                return false
            end
        end
    end
end 
mouse.KeyDown:connect(onKeyDown)

This is the code for the Script in ServerScriptService:

game.ReplicatedStorage.FormEvents.FormEvent.OnServerEvent:Connect(function(Race, Boost, Player)
    print(Race)
    print(Boost)
    print(Player)
    local Character = workspace:FindFirstChild(Player.Name)
    if Character.UpperTorso.Aura then
        return false
    else
        local Aura = Instance.new("ParticleEmitter")
        Aura.Parent = Character.UpperTorso
        Aura.Name = "Aura"
        Aura.Color = Color3.fromRGB(170,0,0)
        Aura.Size = 10
        Aura.Texture = 'https://www.roblox.com/library/347913778/ssj-aura'
        Aura.Transparency = .95
        Aura.LockedToPart = true
        Aura.Lifetime = 0.2
        Aura.Rate = 31
    end
end)

It all works until it reaches the Character portion, can anybody help?

2 answers

Log in to vote
1
Answered by 4 years ago

It's been a while since I've worked in studio, but i think the problem is transferring over the variables using the event, whenever you receive an event fire on the server side the very first variable is always the player, so it might look like this:

game.ReplicatedStorage.FormEvents.FormEvent.OnServerEvent:Connect(function(Player, Race, Boost)
    print(Race)
    print(Boost)
    print(Player)
    local Character = workspace:FindFirstChild(Player.Name)
    if Character.UpperTorso.Aura then
        return false
    else
        local Aura = Instance.new("ParticleEmitter")
        Aura.Parent = Character.UpperTorso
        Aura.Name = "Aura"
        Aura.Color = Color3.fromRGB(170,0,0)
        Aura.Size = 10
        Aura.Texture = 'https://www.roblox.com/library/347913778/ssj-aura'
        Aura.Transparency = .95
        Aura.LockedToPart = true
        Aura.Lifetime = 0.2
        Aura.Rate = 31
    end
end)

Forgive if I'm wrong, but this is the only thing I could find wrong with this.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello.

The "Player" argument is automatically passed as the first parameter of the OnServerEvent event. Also, game.Loaded is an event, so you can't use it in a repeat loop. Instead, use the function game:IsLoaded(). It returns a boolean value.

Improvements:

  1. Use UserInputService instead of Mouse.KeyDown as the mouse is deprecated and shouldn't be used unless getting the mouse CFrame, target, or position.

  2. Use Player.Character instead of using workspace:FindFirstChild(Player.Name).

  3. Use Instance:FindFirstChild() instead of using the conditional dot operator to prevent the script from erroring the the player doesn't have an aura.

Below are the fixed scripts:

LocalScript:

repeat wait() until game:IsLoaded()
local UserInputService = game:GetService("UserInputService")

local mouse = game.Players.LocalPlayer:GetMouse()
local Race = script.Race.Value
local Boost = script.Boost.Value
local Player = game.Players.LocalPlayer

local function onInputBegan(input, gameProcessedEvent)
    if not gameProcessedEvent then
        if input.KeyCode == Enum.KeyCode.F then
            if Player.Backpack.ServerTraits.Race.Value == script.Race.Value then
                game.ReplicatedStorage.FormEvents.FormEvent:FireServer(Race, Boost)
            else
                return false
            end
        end
    end
end 

UserInputService.InputBegan:Connect(onInputBegan)

ServerScript:

game.ReplicatedStorage.FormEvents.FormEvent.OnServerEvent:Connect(function(Player, Race, Player)
    print(Race) 
    print(Boost)
    print(Player) 
    local Character = Player.Character
    if Character.UpperTorso:FindFirstChild("Aura") then
        return false
    else
        local Aura = Instance.new("ParticleEmitter")
    Aura.Name = "Aura"
        Aura.Color = Color3.fromRGB(170,0,0)
        Aura.Size = 10
        Aura.Texture = 'https://www.roblox.com/library/347913778/ssj-aura'
        Aura.Transparency = .95
        Aura.LockedToPart = true
        Aura.Lifetime = 0.2
        Aura.Rate = 31
    Aura.Parent = Character.UpperTorso
    end
end)

Please upvote and accept this answer if it helps.

Answer this question