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

My on touch script do'sent work. Is there anything wrong?

Asked by 4 years ago

Somehow this dosent work. And also theres no errors! Can somebody help me?

function onTouched(hit)

script.Parent.Parent.Parent.StarterGui.IceCream.Up_On_The_Chocolate.Visible = true
wait(6)
script.Parent.Parent.Parent.StarterGui.IceCream.Up_On_The_Chocolate.Visible = false
wait(0.1)
script.Parent:Destroy()







end

script.Parent.Touched:Connect(onTouched)
0
ty snowpototoy 37 — 4y

2 answers

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

try this

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        Player.PlayerGui.IceCream.Up_On_The_Chocolate.Visible = true
        wait(6)
        Player.PlayerGui.IceCream.Up_On_The_Chocolate.Visible = false
        wait(0.1)
        script.Parent:Destroy()
    end
end

script.Parent.Touched:Connect(onTouched())

because you want to call a function you have to add () in the end of the function name like in the last line and also i edited it so it check if a player touch it.

if i was you i would use this script:

local IsTouched = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and IsTouched == false then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        player.PlayerGui.IceCream.Up_On_The_Chocolate.Visible = true
        wait(6)
        player.PlayerGui.IceCream.Up_On_The_Chocolate.Visible = true
        wait(0.1)
        script.Parent:Destroy()
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Small mistake, you're trying to edit a gui from the StarterGui, which doesn't work. The StarterGui just places guis on the player's screen once they join. If you want to edit a player's gui you have to edit it from the PlayerGui, which would be located with game.Players["PlayerNameHere"].PlayerGui. A little bit of extra work is required to do that effectively though.

We're gonna have to dabble into RemoteEvents, which allow for communication between server and client sided scripts. Even if you can, avoid editing player's guis server sided. Assuming that the part your script is in is a server script, we're gonna have to 1: Create the remote event, 2: Get the player who touched the part and affect their gui.

local rs = game:GetService("ReplicatedStorage") -- Getting the ReplicatedStorage - basically a storage dump that both server scripts and local scripts can see
local event = Instance.new("RemoteEvent") -- Create the remote event
event.Name = "IceCreamGuiEvent"
event.Parent = rs

function onTouched(hit)

    if game.Players:GetPlayerFromCharacter(hit.Parent) then -- Check if the parent of the touching part is a player's character
        local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Make the player a variable
        event:FireClient(player) -- Activate the signal the localscript is waiting for
        wait(6.1)
        script.Parent:Destroy()
    end
end

script.Parent.Touched:Connect(onTouched)

Now this next part should be in a localscript in StarterPlayerScripts (which is below StarterPlayer).

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("IceCreamGuiEvent") -- Get that remote event we created earlier
local player = game.Players.LocalPlayer
local pgui = player.PlayerGui

event.OnClientEvent:Connect(function() -- This function will activate whenever the FireClient function is called server side
    pgui.IceCream.Up_On_The_Chocolate.Visible = true
    wait(6)
    pgui.IceCream.Up_On_The_Chocolate.Visible = false
end)
0
you are right he was changing it inside the startergui and not the playergui, i didn't notice it dionsyran2 66 — 4y
0
i edited my post so it should work now dionsyran2 66 — 4y
0
i want rep :( snowpototoy 37 — 4y

Answer this question