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

UserInputService. Is my syntax broken?

Asked by 4 years ago

Please someone help me why this things is not working well. I am trying my best but i dont know how to fix it. Thank you :D

--//Starterpack Local Script\\--
repeat wait() until game.Players.LocalPlayer.Character

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local char = player.Character
local leaderstats = game:GetService("ServerScriptService"):WaitForChild("leaderstats")

userinputservice.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        local anim = Instance.new("Animation")
        anim.AnimationId = "rbxassetid://04480814830"
        local PlayAnim = char.Humanoid:LoadAnimation(anim)
        PlayAnim:Play()
        leaderstats.power.Value = leaderstats.power.Value + 1
        wait(3)
    end
end)

And this is my Leaderstats.

--//ServerScriptService Script\\--
local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("PowerStats") -- 

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local power = Instance.new("IntValue", folder)
    power.Name = "Pow"
    power.Value = ds:GetAsync(plr.UserId) or 0
    power.Changed:Connect(function()
        ds:SetAsync(plr.UserId, power.Value)
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync(plr.UserId, plr.leaderstats.Money.Value) 
end)
1
What’s not working...? Ziffixture 6913 — 4y
1
^ Doesn't seem to be an error with UserInputService. Probably the code within the event. namespace25 594 — 4y
0
did you make the animation you're using? Robowon1 323 — 4y

2 answers

Log in to vote
0
Answered by
cegberry 432 Moderation Voter
4 years ago

So, the problem is, you are trying to access ServerScriptService in a LocalScript, however LocalScripts, cannot access server only instances, hence the names containing "Server", try storing your leaderstats in ReplicatedStorage, where both the client and the server can retrieve data.

Ad
Log in to vote
0
Answered by 4 years ago

I think the problem is that on the 2nd script (line 6), you inserted something named "leaderstats" into the player, but in the 1st script at line 7, you got that "leaderstats" in the ServerScriptService. Also, you inserted in that leaderstats a IntValue named "Pow", but in the 1st script you wrote "leaderstats.Power", although it was named "Pow".

There is another error. In the 15th line in the 1st script, you added 1 to the value of "Pow". But you wrote this in a local script. So it will not change the value of "Pow". To do this, you have to send a signal from that localscript to a normal script with a remote event. Insert a remoteEvent in ReplictedStorage (name it whatever you want) and insert a normal script into Workspace.

So you can replace your 1st script by this:

repeat 
    wait() 
until game.Players.LocalPlayer.Character

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local char = player.Character
local leaderstats = player.leaderstats

userinputservice.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
           local anim = Instance.new("Animation")
           anim.AnimationId = "rbxassetid://04480814830"
           local PlayAnim = char.Humanoid:LoadAnimation(anim)
           PlayAnim:Play()
           game.ReplicatedStorage:WaitForChild(WHATEVER YOU NAMED THE REMOTE EVENT):FireServer() -- gives a signal
           wait(3)
    end
end)

Then, in the normal script you inserted in Workspace, write this:

game.ReplicatedStorage:WaitForChild(WHATEVER YOU NAMED THE REMOTE EVENT).OnServerEvent:Connect(function(player) -- gets the signal and the player
    player.leaderstats.Pow = player.leaderstats.Pow +1
end)

You don't need to make any changes in the script in the ServerScriptService.

Just to make sure, in the local script (1st script), I don't know why you added a wait(3) at the end of it. If you want to let the player click "e" again before the animation finishes, you have to add a debounce. Replace that 1st script I wrote earlier by this:

repeat 
    wait() 
until game.Players.LocalPlayer.Character

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local char = player.Character
local leaderstats = player.leaderstats
local debounce = false

userinputservice.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        if debounce == false then -- checks if debounce is false
            debounce = true
            local anim = Instance.new("Animation")
                anim.AnimationId = "rbxassetid://04480814830"
                local PlayAnim = char.Humanoid:LoadAnimation(anim)
                PlayAnim:Play()
                game.ReplicatedStorage:WaitForChild(WHATEVER YOU NAMED THE REMOTE EVENT):FireServer() -- gives a signal
                wait(3)
            debounce = false -- setting it to false after 3 seconds to let the animation to finish
        end
    end
end)

Hope this helped!

Answer this question